This commit is contained in:
80
api/app/Exceptions/Handler.php
Normal file
80
api/app/Exceptions/Handler.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Throwable;
|
||||
|
||||
class Handler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* 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',
|
||||
];
|
||||
|
||||
/**
|
||||
* Render an exception into an HTTP response.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Throwable $e
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function render($request, Throwable $e)
|
||||
{
|
||||
if ($e instanceof AuthorizationException) {
|
||||
return response()->json(['message' => 'Você não tem permissão para realizar esta ação.'], Response::HTTP_FORBIDDEN);
|
||||
}
|
||||
|
||||
if ($e instanceof ModelNotFoundException) {
|
||||
return response()->json(['error' => 'Registro não encontrado.'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
if ($e instanceof QueryException) {
|
||||
//return response()->json(['error' => 'Definir uma mensagem para QueryException.'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
if ($e instanceof UnauthorizedHttpException) {
|
||||
return response()->json(['error' => 'Não Autenticado.'], Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
if ($e instanceof ValidationException) {
|
||||
return response()->json($e->errors(), Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
return parent::render($request, $e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the exception handling callbacks for the application.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->reportable(function (Throwable $e) {
|
||||
//TODO: Verificar se precisa implementar algo aqui
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user