2020-11-02 22:45:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2020-11-14 23:05:45 +08:00
|
|
|
|
2020-11-02 22:45:37 +08:00
|
|
|
namespace App\Exception\Handler;
|
|
|
|
|
2021-05-24 11:31:36 +08:00
|
|
|
use App\Cache\Repository\LockRedis;
|
2020-11-29 14:44:11 +08:00
|
|
|
use App\Constants\ResponseCode;
|
2021-05-24 11:31:36 +08:00
|
|
|
use App\Support\MailerTemplate;
|
2020-11-02 22:45:37 +08:00
|
|
|
use Hyperf\Contract\StdoutLoggerInterface;
|
|
|
|
use Hyperf\ExceptionHandler\ExceptionHandler;
|
|
|
|
use Hyperf\HttpMessage\Stream\SwooleStream;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Throwable;
|
2020-11-14 23:05:45 +08:00
|
|
|
|
2020-11-02 22:45:37 +08:00
|
|
|
class AppExceptionHandler extends ExceptionHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var StdoutLoggerInterface
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
public function __construct(StdoutLoggerInterface $logger)
|
|
|
|
{
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle(Throwable $throwable, ResponseInterface $response)
|
|
|
|
{
|
|
|
|
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
|
|
|
|
$this->logger->error($throwable->getTraceAsString());
|
2020-11-04 11:57:16 +08:00
|
|
|
|
2020-11-29 14:44:11 +08:00
|
|
|
$data = json_encode([
|
2021-04-20 16:30:57 +08:00
|
|
|
'code' => ResponseCode::SERVER_ERROR,
|
2021-05-24 11:31:36 +08:00
|
|
|
'message' => 'Internal Server Error.',
|
|
|
|
'errors' => config('app_env') == 'dev' ? $throwable->getTrace() : [],
|
2020-11-29 14:44:11 +08:00
|
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
|
2021-05-24 11:31:36 +08:00
|
|
|
$this->sendAdminEmail($throwable);
|
|
|
|
|
2021-04-22 16:54:01 +08:00
|
|
|
return $response->withHeader('Server', 'Lumen IM')->withStatus(500)->withBody(new SwooleStream($data));
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-14 23:05:45 +08:00
|
|
|
/**
|
|
|
|
* @param Throwable $throwable
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-11-02 22:45:37 +08:00
|
|
|
public function isValid(Throwable $throwable): bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-24 11:31:36 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 发送系统报错通知邮件
|
|
|
|
*
|
|
|
|
* @param Throwable $throwable
|
|
|
|
*/
|
|
|
|
public function sendAdminEmail(Throwable $throwable)
|
|
|
|
{
|
2021-05-24 19:03:27 +08:00
|
|
|
if (config('app_env') != 'dev') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-24 11:31:36 +08:00
|
|
|
$error = implode(':', [
|
|
|
|
'error',
|
|
|
|
md5($throwable->getFile() . $throwable->getCode() . $throwable->getLine()),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$adminEmail = config('admin_email');
|
|
|
|
if ($adminEmail && LockRedis::getInstance()->lock($error, 60 * 30)) {
|
|
|
|
try {
|
|
|
|
email()->send(
|
|
|
|
$adminEmail,
|
|
|
|
'系统报错通知',
|
|
|
|
container()->get(MailerTemplate::class)->errorNotice($throwable)
|
|
|
|
);
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|