How can I make Laravel return a custom error for a JSON REST API
go to your app/start/global.php
.
This will convert all errors for 401
and 404
to a custom json error instead of the Whoops stacktrace. Add this:
App::error(function(Exception $exception, $code){ Log::error($exception); $message = $exception->getMessage(); // switch statements provided in case you need to add // additional logic for specific error code. switch ($code) { case 401: return Response::json(array( 'code' => 401, 'message' => $message ), 401); case 404: $message = (!$message ? $message = 'the requested resource was not found' : $message); return Response::json(array( 'code' => 404, 'message' => $message ), 404); }});
This is one of many options to handle this errors.
Making an API it is best to create your own helper like Responser::error(400, 'damn')
that extends the Response
class.
Somewhat like:
public static function error($code = 400, $message = null){ // check if $message is object and transforms it into an array if (is_object($message)) { $message = $message->toArray(); } switch ($code) { default: $code_message = 'error_occured'; break; } $data = array( 'code' => $code, 'message' => $code_message, 'data' => $message ); // return an error return Response::json($data, $code);}
You can pass an array to the returned JSON response:
$returnData = array( 'status' => 'error', 'message' => 'An error occurred!');return Response::json($returnData, 500);
Here is what I use (Laravel 5.2):
According to: https://laravel.com/docs/5.2/errors , we can specify a custom render function for errors in app\Exceptions\Handler.php
. All I did was to change my render function to this:
/** * Render an exception into an HTTP response. * Updated to return json for a request that wantsJson * i.e: specifies * Accept: application/json * in its header * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { if ($request->ajax() || $request->wantsJson()) { return response()->json( $this->getJsonMessage($e), $this->getExceptionHTTPStatusCode($e) ); } return parent::render($request, $e); } protected function getJsonMessage($e){ // You may add in the code, but it's duplication return [ 'status' => 'false', 'message' => $e->getMessage() ]; } protected function getExceptionHTTPStatusCode($e){ // Not all Exceptions have a http status code // We will give Error 500 if none found return method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500; }
After this, all you need do is make sure all your API requests specify the Accept: application/json
header. Hope this helps :)