Created
March 5, 2021 21:58
-
-
Save gentritabazi/98823fc3c343bec462cae70660fdf4be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Exceptions; | |
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | |
use Throwable; | |
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; | |
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 = [ | |
'current_password', | |
'password', | |
'password_confirmation', | |
]; | |
/** | |
* Register the exception handling callbacks for the application. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->reportable(function (Throwable $e) { | |
// | |
}); | |
} | |
/** | |
* 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 $e) | |
{ | |
return $this->renderException($request, $e); | |
} | |
public function renderException($request, $e) | |
{ | |
$status = 500; | |
if ($e instanceof HttpExceptionInterface) { | |
$status = $e->getStatusCode(); | |
} | |
switch ($status) { | |
case 401: | |
$title = strlen($e->getMessage()) ? $e->getMessage() : 'Unauthorized.'; | |
break; | |
case 403: | |
$title = strlen($e->getMessage()) ? $e->getMessage() : 'Forbidden.'; | |
break; | |
case 404: | |
$title = strlen($e->getMessage()) ? $e->getMessage() : 'Not Found.'; | |
break; | |
case 405: | |
$title = strlen($e->getMessage()) ? $e->getMessage() : 'Method Not Allowed.'; | |
break; | |
case 500: | |
$title = (app()->environment('production')) ? 'Whoops, looks like something went wrong.' : $e->getMessage(); | |
break; | |
default: | |
$title = $e->getMessage(); | |
break; | |
} | |
switch ($status) { | |
case 404: | |
$json = [ | |
'status' => $status, | |
'message' => $title | |
]; | |
break; | |
default: | |
$json = [ | |
'status' => $status, | |
'message' => $title, | |
'exception' => (app()->environment('production')) ? '' : (string) $e, | |
'line' => (app()->environment('production')) ? '' : $e->getLine(), | |
'file' => (app()->environment('production')) ? '' : $e->getFile() | |
]; | |
} | |
return response()->json($json, $status); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment