mirror of
https://gitlab.com/izyim/prototypes/lumen-microservices/microservice-products-api.git
synced 2026-08-19 20:34:14 +00:00
96 lines
2.7 KiB
PHP
96 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use App\Traits\ApiResponse;
|
|
use Exception;
|
|
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(env('APP_DEBUG',false)){
|
|
return parent::render($request, $exception);
|
|
}
|
|
|
|
return $this->errorResponse(
|
|
"Unexpected error. Try later!",
|
|
Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
}
|