You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1013 B

6 years ago
  1. <?php
  2. namespace App\Exception\Handler;
  3. use App\Exception\SsdbException;
  4. use Hyperf\ExceptionHandler\ExceptionHandler;
  5. use Psr\Http\Message\ResponseInterface;
  6. use Throwable;
  7. use App\Constants\ErrorCode;
  8. use Hyperf\HttpMessage\Stream\SwooleStream;
  9. class SsdbExceptionHandler extends ExceptionHandler
  10. {
  11. public function handle(Throwable $throwable, ResponseInterface $response)
  12. {
  13. $this->stopPropagation();
  14. $content = json_encode(
  15. [
  16. "status" => 'error',
  17. "code" => ErrorCode::SSDB_ERROR,
  18. "result" => [],
  19. "message" => $throwable->getMessage() ?: ErrorCode::getMessage(ErrorCode::SSDB_ERROR)
  20. ]
  21. );
  22. return $response->withHeader('Content-Type', 'application/json')
  23. ->withStatus($throwable->status)
  24. ->withBody(new SwooleStream($content));
  25. }
  26. public function isValid(Throwable $throwable): bool
  27. {
  28. return $throwable instanceof SsdbException;
  29. }
  30. }