Just change the Handler with the exception of framework, located in app\Exceptions\Handler.php
, changing the method that renders the exceptions, leaving it so:
public function render($request, Exception $e)
{
$json = [
'success' => false,
'error' => [
'code' => $e->getCode(),
'message' => $e->getMessage(),
],
];
return response()->json($json, 400);
}
Basically whenever an exception occurs you are picking up some data such as the error code and its message and returning in JSON. In the production environment this would never appear, as long as it disables the debug
.
But note that this will also bar a request
with wrong validation, and you will not be allowed to pick up the validation messages, to circumvent this we need to respond in json in this case only when the error code is non-zero, because the zero error is related to the validation error, then the code will look like this:
public function render($request, Exception $e)
{
if($e->getCode() != 0) {
$json = [
'success' => false,
'error' => [
'code' => $e->getCode(),
'message' => $e->getMessage(),
],
];
return response()->json($json, 400);
}
return parent::render($request, $e);
}