Files
Sio-Back/app/Exceptions/Handler.php
SIO Admin de656b70a2 feat: Actualizacion sistema SIO Backend
- Nuevo modulo de historial de cambios (ServicioHistorial)
- Observer para tracking automatico de cambios en servicios
- Correccion de variables auxiliar en ServiciosController
- Actualizacion de configuraciones y migraciones
- Endpoint para consultar historial de cambios

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-17 23:01:55 +00:00

113 lines
3.4 KiB
PHP
Executable File

<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenBlacklistedException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if($exception instanceof NotFoundHttpException){
return response()->notFound('Ruta no encontrada');
}
if($exception instanceof ModelNotFoundException){
return response()->notFound('Recurso no encontrado');
}
if($exception instanceof ValidationException){
$errors = $exception->validator->errors();
return response()->unprocessable('Parametros inválidos',$errors);
}else if($exception instanceof TokenExpiredException) {
return response()->unauthorized($exception->getMessage());
}else if($exception instanceof UnauthorizedHttpException){
return response()->unauthorized($exception->getMessage());
}else if($exception instanceof TokenBlacklistedException){
return response()->unauthorized($exception->getMessage());
}else if($exception instanceof TokenInvalidException){
return response()->badRequest($exception->getMessage());
}
else if($exception instanceof JWTException) {
return response()->badRequest($exception->getMessage() );
}
return parent::render($request, $exception);
}
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest(route('login'));
}
}