<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\ValidationException;
use ReflectionException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
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 \Exception $exception
* @return void
* @throws \Exception
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP jsonResponse.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exceptionxception
* @return \Symfony\Component\HttpFoundation\Response
*/
public function render($request, Exception $exception)
{
if($exception instanceof MethodNotAllowedHttpException) {
return $this->jsonResponse(
'NotFound',
'Not Found.',
404
);
}
if($exception instanceof ModelNotFoundException) {
$model = str_replace('App\\Models\\', '', $exception->getModel());
return $this->jsonResponse(
'ModelNotFound',
$model.' not found.',
404);
}
if($exception instanceof AuthorizationException) {
return $this->jsonResponse(
'InvalidCredentials',
'Credentials are invalid.',
400);
}
if($exception instanceof ValidationException) {
return $this->jsonResponse(
'ValidationError',
$exception->errors(),
422
);
}
if($exception instanceof ReflectionException) {
return $this->jsonResponse(
'ClassNotFound',
'Class not founded!',
404
);
}
return $this->jsonResponse(
'UnexpectedException',
'Unexpected Exception. Try later',
500);
}
private function jsonResponse($errorNameCode, $errorMsg, $httpStatus, $addon = false)
{
return response()->json([
'code' => $errorNameCode,
'message' => $errorMsg
], $httpStatus);
}
}