Skip to content

Instantly share code, notes, and snippets.

@sirolad
Created September 18, 2017 18:28
Show Gist options
  • Save sirolad/7dba2b1d019e0603d8f16a4b382376cd to your computer and use it in GitHub Desktop.
Save sirolad/7dba2b1d019e0603d8f16a4b382376cd to your computer and use it in GitHub Desktop.
Modified global exception for JSON in app/Exceptions/Hander.php file
<?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