149 lines
4.7 KiB
PHP
149 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use App\Traits\ApiResponser;
|
|
use GuzzleHttp\Exception\ClientException;
|
|
use GuzzleHttp\Exception\ServerException;
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
|
|
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 \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
|
*
|
|
* @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()));
|
|
|
|
return $this->errorResponse(trans('errors.model_not_found',['model'=>$model]),
|
|
Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
if($exception instanceof AuthorizationException)
|
|
{
|
|
return $this->errorResponse($exception->getMessage(),Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
if($exception instanceof ValidationException)
|
|
{
|
|
$errors = $exception->validator->errors()->getMessages();
|
|
$message = '';
|
|
foreach ($errors as $key => $val) {
|
|
foreach ($val as $validation) {
|
|
$message .= trans('errors.validation_error', ['field' => $key, 'validation' => $validation]);
|
|
}
|
|
$message .= "\n";
|
|
}
|
|
return $this->errorResponse($message, Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
}
|
|
|
|
|
|
if($exception instanceof AuthenticationException)
|
|
{
|
|
return $this->errorResponse($exception->getMessage(),Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
if ($exception instanceof QueryException)
|
|
{
|
|
return $this->errorResponse($exception->getMessage(),Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
if ($exception instanceof ServerException)
|
|
{
|
|
return $this->errorResponse($exception->getMessage(),Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
if ($exception instanceof \ErrorException)
|
|
{
|
|
return $this->errorResponse($exception->getMessage(),Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
if ( $exception instanceof ClientException)
|
|
{
|
|
$message = $exception->getResponse()->getBody()->getContents();
|
|
$error =json_decode($message);
|
|
$code = $exception->getCode();
|
|
|
|
if($error){
|
|
if (isset($error->message)) {
|
|
$message = json_decode($error->message);
|
|
if (isset($message->errorMessage))
|
|
return $this->errorResponse($message->errorMessage, $code);
|
|
return $this->errorResponse($error->message, $code);
|
|
}
|
|
if (isset($error->error)) {
|
|
$message = json_decode($error->error);
|
|
if (isset($message->message))
|
|
return $this->errorResponse($message->message, $code);
|
|
return $this->errorResponse($error->error, $code);
|
|
}
|
|
return $this->errorResponse($error, $code);
|
|
} else
|
|
return $this->errorResponse($message,$code);
|
|
}
|
|
|
|
if( env('APP_DEBUG', false))
|
|
{
|
|
return parent::render($request,$exception);
|
|
}
|
|
|
|
return $this->errorResponse(trans('errors.unexpected_error'),
|
|
Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|