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.

58 lines
1.7 KiB

  1. <?php
  2. namespace App\Utilities\Exceptions;
  3. use Throwable;
  4. use ReflectionClass;
  5. use ReflectionMethod;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Validation\ValidationException;
  8. use Illuminate\Auth\Access\AuthorizationException;
  9. use Illuminate\Database\Eloquent\ModelNotFoundException;
  10. use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
  11. use Symfony\Component\HttpKernel\Exception\HttpException;
  12. class Handler extends ExceptionHandler
  13. {
  14. /**
  15. * A list of the exception types that should not be reported.
  16. *
  17. * @var array
  18. */
  19. protected $dontReport = [
  20. AuthorizationException::class,
  21. HttpException::class,
  22. ModelNotFoundException::class,
  23. ValidationException::class,
  24. ];
  25. public function report(Throwable $exception)
  26. {
  27. // A trick that I took from Laravel macroable trait
  28. $methods = (new ReflectionClass($exception))->getMethods(
  29. ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
  30. );
  31. $result = [];
  32. foreach ($methods as $method) {
  33. // invoke the method so we can collect the result of execution it
  34. $result[$method->name] = $method->invoke($exception);
  35. }
  36. // Clear the unnecessary method
  37. unset($result['getTrace']);
  38. unset($result['__toString']);
  39. // clear the null values then encode it as json
  40. // so we can decode it as an object in the Monolog Processor
  41. $result = json_encode(array_filter($result));
  42. return Log::emergency($result);
  43. }
  44. public function render($request, Throwable $exception)
  45. {
  46. return parent::render($request, $exception);
  47. }
  48. }