|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/lecturebazaar.com/app/Exceptions/ |
Upload File : |
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
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',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
if ($request->is('api/*')) {
return $this->renderApi($request, $exception);
}
return parent::render($request, $exception);
}
public function renderApi($request, Throwable $e)
{
if ($e instanceof MethodNotAllowedHttpException) {
$status = Response::HTTP_METHOD_NOT_ALLOWED;
$e = new MethodNotAllowedHttpException([], 'HTTP_METHOD_NOT_ALLOWED', $e);
} elseif ($e instanceof NotFoundHttpException) {
$status = Response::HTTP_NOT_FOUND;
$e = new NotFoundHttpException('HTTP_NOT_FOUND', $e);
} elseif ($e instanceof AuthorizationException) {
$status = Response::HTTP_FORBIDDEN;
$e = new AuthorizationException('HTTP_FORBIDDEN', $status);
} elseif ($e instanceof \Dotenv\Exception\ValidationException && $e->getResponse()) {
$status = Response::HTTP_BAD_REQUEST;
$e = new \Dotenv\Exception\ValidationException('HTTP_BAD_REQUEST', $status, $e);
} elseif ($e instanceof ValidationException && $e->getResponse()) {
return $e->getResponse();
} else {
if (env('APP_DEBUG')) {
return parent::render($request, $e);
}
$status = Response::HTTP_INTERNAL_SERVER_ERROR;
$e = new HttpException($status, 'HTTP_INTERNAL_SERVER_ERROR');
}
return response()->json([
'success' => false,
'status' => $status,
'message' => $e->getMessage()
], $status);
}
}