ExceptionReport.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Http\Helpers;
  3. use App\Exceptions\IsAlreadyException;
  4. use App\Exceptions\MeetExpiredException;
  5. use App\Exceptions\MeetNoPermissionException;
  6. use App\Exceptions\MeetNotStartException;
  7. use ErrorException;
  8. use Exception;
  9. use Illuminate\Auth\Access\AuthorizationException;
  10. use Illuminate\Auth\AuthenticationException;
  11. use Illuminate\Database\Eloquent\ModelNotFoundException;
  12. use Illuminate\Database\QueryException;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Validation\ValidationException;
  15. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  18. use Tymon\JWTAuth\Exceptions\TokenInvalidException;
  19. class ExceptionReport
  20. {
  21. use ApiResponse;
  22. /**
  23. * @var Exception
  24. */
  25. public $exception;
  26. /**
  27. * @var Request
  28. */
  29. public $request;
  30. /**
  31. * @var
  32. */
  33. protected $report;
  34. /**
  35. * ExceptionReport constructor.
  36. * @param Request $request
  37. * @param Exception $exception
  38. */
  39. function __construct(Request $request, Exception $exception)
  40. {
  41. $this->request = $request;
  42. $this->exception = $exception;
  43. }
  44. /**
  45. * @var array
  46. */
  47. //当抛出这些异常时,可以使用我们定义的错误信息与HTTP状态码
  48. //可以把常见异常放在这里
  49. public $doReport = [
  50. AuthenticationException::class => ['未登录或登录状态失效', 401],
  51. ModelNotFoundException::class => ['数据不存在', 404],
  52. AuthorizationException::class => ['没有此权限', 403],
  53. ValidationException::class => [],
  54. UnauthorizedHttpException::class => ['未登录或登录状态失效', 401],
  55. TokenInvalidException::class => ['未登录或登录状态失效', 401],
  56. NotFoundHttpException::class => ['没有找到该页面', 404],
  57. MethodNotAllowedHttpException::class => ['访问方式不正确', 405],
  58. ErrorException::class => ['服务器内部错误', 500],
  59. QueryException::class => ['参数错误', 500],
  60. IsAlreadyException::class => ['数据已经存在', 500],
  61. MeetNotStartException::class => ['讲座未开始', 500],
  62. MeetExpiredException::class => ['讲座已结束', 500],
  63. MeetNoPermissionException::class => ['您没有权限参加该讲座', 500],
  64. ];
  65. public function register($className, callable $callback)
  66. {
  67. $this->doReport[$className] = $callback;
  68. }
  69. /**
  70. * @return bool
  71. */
  72. public function shouldReturn()
  73. {
  74. foreach (array_keys($this->doReport) as $report) {
  75. if ($this->exception instanceof $report) {
  76. $this->report = $report;
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. /**
  83. * @param Exception $e
  84. * @return static
  85. */
  86. public static function make(Exception $e)
  87. {
  88. return new static(\request(), $e);
  89. }
  90. /**
  91. * @return mixed
  92. */
  93. public function report()
  94. {
  95. if ($this->exception instanceof ValidationException) {
  96. return $this->failed(current($this->exception->errors()), $this->exception->status);
  97. }
  98. $message = $this->doReport[$this->report];
  99. return $this->failed($message[0], $message[1]);
  100. }
  101. public function prodReport()
  102. {
  103. return $this->failed('服务器错误', '500');
  104. }
  105. }