Handler.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Exceptions;
  3. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  4. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use Illuminate\Database\QueryException;
  7. use Throwable;
  8. class Handler extends ExceptionHandler
  9. {
  10. /**
  11. * A list of the exception types that are not reported.
  12. *
  13. * @var array<int, class-string<Throwable>>
  14. */
  15. protected $dontReport = [
  16. //
  17. ];
  18. /**
  19. * A list of the inputs that are never flashed for validation exceptions.
  20. *
  21. * @var array<int, string>
  22. */
  23. protected $dontFlash = [
  24. 'current_password',
  25. 'password',
  26. 'password_confirmation',
  27. ];
  28. /**
  29. * Register the exception handling callbacks for the application.
  30. *
  31. * @return void
  32. */
  33. public function register()
  34. {
  35. $this->reportable(function (Throwable $e) {
  36. //
  37. });
  38. $this->renderable(function (MethodNotAllowedHttpException $e, $request){
  39. $respuesta = json_encode([
  40. "error" => true,
  41. "msg" => "ERR_GLB_USU000: Método no soportado.",
  42. "response" => []
  43. ]);
  44. return response($respuesta, 405)->header('Content-Type', 'application/json');
  45. });
  46. $this->renderable(function (NotFoundHttpException $e, $request){
  47. $respuesta = json_encode([
  48. "error" => true,
  49. "msg" => "ERR_GLB_USU001: La ruta solicitada no existe: " . $request->fullUrl(),
  50. "response" => []
  51. ]);
  52. return response($respuesta, 404)->header('Content-Type', 'application/json');
  53. });
  54. $this->renderable(function (QueryException $e, $request){
  55. $respuesta = json_encode([
  56. "error" => true,
  57. //"msg" => "ERR_GLB_USU002: No se pudo establecer una conexión con la base de datos. Código de error: " . $e->getCode(),
  58. "msg" => "ERR_GLB_USU002: No se pudo establecer una conexión con la base de datos. Código de error: " . $e->getMessage(),
  59. "response" => []
  60. ]);
  61. return response($respuesta, 500)->header('Content-Type', 'application/json');
  62. });
  63. }
  64. }