AppExceptionHandler.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace App\Exception\Handler;
  12. use Hyperf\Contract\StdoutLoggerInterface;
  13. use Hyperf\ExceptionHandler\ExceptionHandler;
  14. use Hyperf\HttpMessage\Stream\SwooleStream;
  15. use Psr\Http\Message\ResponseInterface;
  16. use Throwable;
  17. class AppExceptionHandler extends ExceptionHandler
  18. {
  19. public function __construct(protected StdoutLoggerInterface $logger)
  20. {
  21. }
  22. public function handle(Throwable $throwable, ResponseInterface $response)
  23. {
  24. $this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
  25. $this->logger->error($throwable->getTraceAsString());
  26. return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.'));
  27. }
  28. public function isValid(Throwable $throwable): bool
  29. {
  30. return true;
  31. }
  32. }