Handler.php 2.4 KB

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