Created
September 18, 2017 18:28
-
-
Save sirolad/7dba2b1d019e0603d8f16a4b382376cd to your computer and use it in GitHub Desktop.
Modified global exception for JSON in app/Exceptions/Hander.php file
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 Exception; | |
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | |
class Handler extends ExceptionHandler | |
{ | |
... | |
/** | |
* Render an exception into an HTTP response. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Exception $exception | |
* @return \Illuminate\Http\Response | |
*/ | |
public function render($request, Exception $e) | |
{ | |
if ($request->ajax() || $request->wantsJson()) | |
{ | |
$json = [ | |
'success' => false, | |
'error' => [ | |
'code' => $e->getCode(), | |
'message' => $e->getMessage(), | |
], | |
]; | |
return response()->json($json, 400); | |
} | |
return parent::render($request, $e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment