Handler.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Base\Utils;
  4. use Guanguans\Notify\Factory;
  5. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Validation\ValidationException;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. class Handler extends ExceptionHandler
  10. {
  11. /**
  12. * A list of exception types with their corresponding custom log levels.
  13. *
  14. * @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
  15. */
  16. protected $levels = [
  17. ];
  18. /**
  19. * A list of the exception types that are not reported.
  20. *
  21. * @var array<int, class-string<\Throwable>>
  22. */
  23. protected $dontReport = [
  24. ];
  25. /**
  26. * A list of the inputs that are never flashed to the session on validation exceptions.
  27. *
  28. * @var array<int, string>
  29. */
  30. protected $dontFlash = [
  31. 'current_password',
  32. 'password',
  33. 'password_confirmation',
  34. ];
  35. /**
  36. * Register the exception handling callbacks for the application.
  37. *
  38. * @return void
  39. */
  40. public function register()
  41. {
  42. $this->reportable(function (ClientException $e) {
  43. logger()->warning($e->getMessage(), array_merge(request()->all(), request()->headers->all()));
  44. return false;
  45. });
  46. $this->reportable(function (\Throwable $e) {
  47. if (!Utils::isDev()) {
  48. Factory::weWork()
  49. ->setToken('c7264726-9a21-4e5c-b0e8-9020a9b4ca62')
  50. ->setMessage((new \Guanguans\Notify\Messages\WeWork\TextMessage([
  51. 'content' => $e->getMessage() . "\n" . $e->getTraceAsString(),
  52. ])))->send();
  53. }
  54. return false;
  55. });
  56. $this->renderable(function (ClientException $e) {
  57. return response()->json([
  58. 'code' => 400,
  59. "error" => $e->getMessage(),
  60. ], 200);
  61. });
  62. $this->renderable(function (ValidationException $e) {
  63. $msg = Arr::first($e->errors());
  64. return response()->json([
  65. "error" => $msg[0],
  66. "code" => 400,
  67. ]);
  68. });
  69. $this->renderable(function (NotFoundHttpException $e) {
  70. return response()->json([
  71. "error" => "数据不存在",
  72. "code" => 400,
  73. ]);
  74. });
  75. }
  76. }