hyperf-chat/app/Exception/Handler/AppExceptionHandler.php

42 lines
1.0 KiB
PHP
Raw Normal View History

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;
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-02 22:45:37 +08:00
return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.'));
}
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;
}
}