117 lines
3.8 KiB
PHP
Executable File
117 lines
3.8 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
use Throwable;
|
|
|
|
class Handler extends ExceptionHandler
|
|
{
|
|
/**
|
|
* A list of exception types with their corresponding custom log levels.
|
|
*
|
|
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
|
|
*/
|
|
protected $levels = [
|
|
//
|
|
];
|
|
|
|
/**
|
|
* A list of the exception types that are not reported.
|
|
*
|
|
* @var array<int, class-string<\Throwable>>
|
|
*/
|
|
protected $dontReport = [
|
|
//
|
|
];
|
|
|
|
/**
|
|
* A list of the inputs that are never flashed for validation exceptions.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $dontFlash = [
|
|
'current_password',
|
|
'password',
|
|
'password_confirmation',
|
|
];
|
|
|
|
/**
|
|
* Register the exception handling callbacks for the application.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
$this->reportable(function (Throwable $e) {
|
|
//
|
|
});
|
|
}
|
|
|
|
public function render($request, Throwable $exception)
|
|
{
|
|
if ($request->wantsJson()) {
|
|
if ($exception instanceof AuthenticationException) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'statusCode' => Response::HTTP_UNAUTHORIZED,
|
|
'message' => 'Unauthenticated',
|
|
'errors' => [
|
|
'Unauthenticated'
|
|
]
|
|
], Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
if ($exception instanceof AuthorizationException) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'statusCode' => Response::HTTP_FORBIDDEN,
|
|
'message' => 'This action is unauthorized.',
|
|
'errors' => [
|
|
'This action is unauthorized.'
|
|
]
|
|
], Response::HTTP_FORBIDDEN);
|
|
}
|
|
|
|
if ($exception instanceof NotFoundHttpException) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'statusCode' => Response::HTTP_NOT_FOUND,
|
|
'message' => 'Route Not Found',
|
|
'errors' => [
|
|
'Route Not Found'
|
|
]
|
|
], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
if ($exception instanceof ModelNotFoundException) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'statusCode' => Response::HTTP_NOT_FOUND,
|
|
'message' => 'Record for ' . str_replace('App', '', str_replace('\\Models\\', '', $exception->getModel())) . ' not found',
|
|
'errors' => [
|
|
'Record for ' . str_replace('App', '', str_replace('\\Models\\', '', $exception->getModel())) . ' not found'
|
|
]
|
|
], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
if ($exception instanceof ValidationException) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'statusCode' => Response::HTTP_UNPROCESSABLE_ENTITY,
|
|
'message' => 'The given data was invalid.',
|
|
'errors' => collect($exception->errors())->flatten()
|
|
], Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
}
|
|
}
|
|
|
|
return parent::render($request, $exception);
|
|
}
|
|
}
|