转到您的 .app/start/global.php
这会将和的所有错误转换为自定义 json 错误,而不是 Whoops 堆栈跟踪。添加以下内容:401
404
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);
}
});
这是处理此错误的众多选项之一。
制作 API 时,最好创建自己的帮助程序,这样可以扩展类。Responser::error(400, 'damn')
Response
有点像:
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);
}