ldc-mesbr-app/app/Exceptions/Handler.php

68 lines
1.8 KiB
PHP
Raw Normal View History

2019-01-15 14:17:15 +01:00
<?php
namespace App\Exceptions;
2021-04-20 19:51:30 +02:00
use App\Http\Resources\StatusResource;
2019-01-15 14:17:15 +01:00
use Exception;
2021-04-20 19:51:30 +02:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2019-01-15 14:17:15 +01:00
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
2021-04-20 19:51:30 +02:00
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
2021-04-20 12:22:04 +02:00
use Throwable;
2019-01-15 14:17:15 +01:00
class Handler extends ExceptionHandler
{
2021-04-27 17:29:47 +02:00
2019-01-15 14:17:15 +01:00
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
2021-04-20 12:22:04 +02:00
* @param \Throwable $exception
2019-01-15 14:17:15 +01:00
* @return void
*/
2021-04-20 12:22:04 +02:00
public function report(Throwable $exception)
2019-01-15 14:17:15 +01:00
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
2021-04-20 12:22:04 +02:00
* @param \Throwable $exception
2021-04-20 19:51:30 +02:00
* @return \Symfony\Component\HttpFoundation\Response
2019-01-15 14:17:15 +01:00
*/
2021-04-20 12:22:04 +02:00
public function render($request, Throwable $exception)
2019-01-15 14:17:15 +01:00
{
2021-04-27 17:29:47 +02:00
if ($request->wantsJson() && $exception instanceof ModelNotFoundException) {
2021-04-20 19:51:30 +02:00
return response()->json(new StatusResource(['status' => 'KO', 'code' => 404, 'message' => 'Resource not found.']), 404);
}
2019-03-12 11:43:50 +01:00
// if ($exception instanceof \ErrorException) {
// $response = response()->view('errors.500', [], 500);
// return $this->toIlluminateResponse($response,$exception);
// }
2019-02-28 09:28:24 +01:00
2019-01-15 14:17:15 +01:00
return parent::render($request, $exception);
}
}