mobilebackendgateway/app/Exceptions/Handler.php

129 lines
3.9 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Exceptions;
use App\Traits\ApiResponser;
use GuzzleHttp\Exception\ClientException;
2020-04-17 15:25:03 +00:00
use GuzzleHttp\Exception\ServerException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use League\OAuth2\Server\Exception\OAuthServerException;
2023-08-01 06:14:28 +00:00
use Symfony\Component\HttpFoundation\Response as ResponseAlias;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;
class Handler extends ExceptionHandler
{
use ApiResponser;
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @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)
{
// return parent::render($request, $exception);
if ($exception instanceof HttpException)
{
$code = $exception->getStatusCode();
$message = Response::$statusTexts[$code];
return $this->errorResponse($message,$code);
}
if($exception instanceof ModelNotFoundException)
{
$model = strtolower(class_basename($exception->getModel()));
2020-04-28 17:35:28 +00:00
return $this->errorResponse(trans('errors.model_not_found',['model'=>$model]),
2023-08-01 06:14:28 +00:00
ResponseAlias::HTTP_NOT_FOUND);
}
if($exception instanceof AuthorizationException)
{
return $this->errorResponse($exception->getMessage(),Response::HTTP_UNAUTHORIZED);
}
if($exception instanceof ValidationException)
{
$errors = $exception->validator->errors()->getMessages();
2023-08-01 06:14:28 +00:00
return $this->errorResponse($errors, ResponseAlias::HTTP_UNPROCESSABLE_ENTITY);
}
if ( $exception instanceof ClientException)
{
2020-05-01 19:12:24 +00:00
$message = $exception->getResponse()->getBody()->getContents();
$error =json_decode($message) ;
$code = $exception->getCode();
2020-05-01 19:12:24 +00:00
if($error)
return $this->errorResponse($error->error,$code);
else
return $this->errorResponse($message,$code);
}
if($exception instanceof AuthenticationException)
{
2023-08-01 06:14:28 +00:00
return $this->errorResponse($exception->getMessage(), ResponseAlias::HTTP_UNAUTHORIZED);
}
if ($exception instanceof OAuthServerException)
{
2023-08-01 06:14:28 +00:00
return $this->errorResponse($exception->getMessage(), ResponseAlias::HTTP_INTERNAL_SERVER_ERROR);
}
2020-04-17 15:25:03 +00:00
if ($exception instanceof ServerException)
{
$errorBody = $exception->getResponse()->getBody()->getContents();
$error = json_decode($errorBody);
2023-08-01 06:14:28 +00:00
$message = $error?->error ?? $exception->getMessage();
$code = $error?->status ?? ResponseAlias::HTTP_INTERNAL_SERVER_ERROR;
return $this->errorResponse($message,$code);
2020-04-17 15:25:03 +00:00
}
if( env('APP_DEBUG', false))
{
return parent::render($request,$exception);
}
2020-04-28 17:35:28 +00:00
return $this->errorResponse(trans('errors.unexpected_error'),
Response::HTTP_INTERNAL_SERVER_ERROR);
}
}