Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Laravel return a custom error for a JSON REST API

I'm developing some kind of RESTful API. When some error occurs, I throw an App::abort($code, $message) error.

The problem is: I want him to throw a json formed array with keys "code" and "message", each one containing the above mentioned data.

Array (     [code] => 401     [message] => "Invalid User" ) 

Does any one knows if it's possible, and if it is, how I do it?

like image 402
Dennis Braga Avatar asked Mar 14 '14 19:03

Dennis Braga


People also ask

How do I return a response in laravel?

Laravel provides several different ways to return response. Response can be sent either from route or from controller. The basic response that can be sent is simple string as shown in the below sample code. This string will be automatically converted to appropriate HTTP response.

How do I enable errors in laravel?

This can be achieved with the variable APP_DEBUG set in the environment file . env stored at the root of the application. For local environment the value of APP_DEBUG should be true but for production it needs to be set to false to hide errors.

How do I respond to a status code in laravel?

You can use http_response_code() to set HTTP response code. If you pass no parameters then http_response_code will get the current status code. If you pass a parameter it will set the response code.


2 Answers

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); } 
like image 151
majidarif Avatar answered Sep 22 '22 15:09

majidarif


You can pass an array to the returned JSON response:

$returnData = array(     'status' => 'error',     'message' => 'An error occurred!' ); return Response::json($returnData, 500); 
like image 21
Philip Avatar answered Sep 21 '22 15:09

Philip