Created
March 11, 2019 10:07
-
-
Save elmarputz/a4822f8af95f9f9f244f667baaebc598 to your computer and use it in GitHub Desktop.
Render Method
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
/** | |
* 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) | |
{ | |
/** | |
* added by elmar | |
*/ | |
// If the request wants JSON (AJAX doesn't always want JSON) | |
if ($request->wantsJson()) | |
{ | |
// Define the response | |
$response = [ | |
'errors' => 'Sorry, something went wrong.' | |
]; | |
// If the app is in debug mode | |
if (config('app.debug')) | |
{ | |
// Add the exception class name, message and stack trace to response | |
$response['exception'] = get_class($exception); // Reflection might be better here | |
$response['message'] = $exception->getMessage(); | |
$response['trace'] = $exception->getTrace(); | |
} | |
// Default response of 400 | |
$status = 400; | |
// If this exception is an instance of HttpException | |
if ($exception instanceof HttpException) | |
{ | |
// Grab the HTTP status code from the Exception | |
$status = $exception->getStatusCode(); | |
} | |
// Return a JSON response with the response array and status code | |
return response()->json($response, $status); | |
} | |
// Default to the parent class' implementation of handler | |
return parent::render($request, $exception); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment