mirror of
https://gitlab.com/izyim/prototypes/lumen-microservices/microservice-api-gateway.git
synced 2026-08-19 04:24:15 +00:00
initial commit
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use App\Traits\ApiResponse;
|
||||
use Exception;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class Handler extends ExceptionHandler
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
/**
|
||||
* 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 \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 HttpException) {
|
||||
$errorCode = $exception->getStatusCode();
|
||||
$errorMessage = Response::$statusTexts[$errorCode];
|
||||
return $this->errorResponse($errorMessage, $errorCode);
|
||||
}
|
||||
|
||||
if ($exception instanceof ModelNotFoundException) {
|
||||
$model = strtolower(class_basename($exception->getModel()));
|
||||
return $this->errorResponse(
|
||||
"Does not exist any instance of {$model} with a given id",
|
||||
Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
if ($exception instanceof AuthorizationException) {
|
||||
return $this->errorResponse(
|
||||
$exception->getMessage(),
|
||||
Response::HTTP_FORBIDDEN);
|
||||
}
|
||||
|
||||
if ($exception instanceof AuthenticationException) {
|
||||
return $this->errorResponse(
|
||||
$exception->getMessage(),
|
||||
Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
if ($exception instanceof ValidationException) {
|
||||
$errors = $exception->validator->errors()->getMessages();
|
||||
return $this->errorResponse(
|
||||
$errors,
|
||||
Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
if ($exception instanceof ClientException) {
|
||||
$errorMessage = $exception->getResponse()->getBody();
|
||||
$errorCode = $exception->getCode();
|
||||
|
||||
return $this->errorMessage($errorMessage, $errorCode);
|
||||
}
|
||||
|
||||
|
||||
if (env('APP_DEBUG', false)) {
|
||||
return parent::render($request, $exception);
|
||||
}
|
||||
|
||||
return $this->errorResponse(
|
||||
"Unexpected error. Try later!",
|
||||
Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user