当前位置: 首页 > 知识库问答 >
问题:

Laravel Dingo Api-如何从Api控制器返回JSON格式的错误响应?

郁烨
2023-03-14

在我的routes.php中,我有以下内容:

$apiSettings = [
    'version' => 'v1',
    'prefix' => 'api',
    'protected' => true
];   
Route::api($apiSettings, function() {
    Route::get('venue', 'ApiDataController@venue');
});
public function venue(){

    try {
        //Some code that returns an exception
    } catch(someexception $e) {
        //How do I return the exception such that Dingo will parse it into a proper JSON response?
    }
    $response = Venue::with('address')->get();
    return $response;

}
try {
    //some code that returns an exception
} catch(someexception $e) {
    $response = array(
        'message' => 'some random exception message'
    );
    return Response::json($response, 403);
}
public function venue(){
    throw new Symfony\Component\HttpKernel\Exception\ConflictHttpException('err);
}
[internal function]: ApiDataController->venue()
#1 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(231):
call_user_func_array(Array, Array)
#2 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(93):
Illuminate\Routing\Controller->callAction('venue', Array)
#3 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(62):
Illuminate\Routing\ControllerDispatcher->call(Object(ApiDataController),
Object(Illuminate\Routing\Route), 'venue')
#4 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(930):
Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route),
Object(Dingo\Api\Http\InternalRequest), 'ApiDataControll...', 'venue')
#5 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#6 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Route.php(105):
call_user_func_array(Object(Closure), Array)
#7 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(996):
Illuminate\Routing\Route->run(Object(Dingo\Api\Http\InternalRequest))
#8 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(964):
Illuminate\Routing\Router->dispatchToRoute(Object(Dingo\Api\Http\InternalRequest))
#9 /vagrant/www/planat-app/vendor/dingo/api/src/Routing/Router.php(147):
Illuminate\Routing\Router->dispatch(Object(Dingo\Api\Http\InternalRequest))
#10 /vagrant/www/planat-app/vendor/dingo/api/src/Dispatcher.php(337): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\InternalRequest))
#11 /vagrant/www/planat-app/vendor/dingo/api/src/Dispatcher.php(278): Dingo\Api\Dispatcher->dispatch(Object(Dingo\Api\Http\InternalRequest))
#12 /vagrant/www/planat-app/vendor/dingo/api/src/Dispatcher.php(213): Dingo\Api\Dispatcher->queueRequest('get', 'venue', Array)
#13 /vagrant/www/planat-app/app/routes.php(51): Dingo\Api\Dispatcher->get('venue')
#14 [internal function]: {closure}()
#15 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Route.php(105):
call_user_func_array(Object(Closure), Array)
#16 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(996):
Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#17 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(964):
Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#18 /vagrant/www/planat-app/vendor/dingo/api/src/Routing/Router.php(147):
Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#19 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(738):
Dingo\Api\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#20 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(708):
Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#21 /vagrant/www/planat-app/vendor/dingo/api/src/Http/Middleware/RateLimit.php(97):
Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request),
1, true)
#22 /vagrant/www/planat-app/vendor/dingo/api/src/Http/Middleware/Authentication.php(102):
Dingo\Api\Http\Middleware\RateLimit->handle(Object(Illuminate\Http\Request),
1, true)
#23 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Session/Middleware.php(72):
Dingo\Api\Http\Middleware\Authentication->handle(Object(Illuminate\Http\Request),
1, true)
#24 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Cookie/Queue.php(47):
Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request),
1, true)
#25 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Cookie/Guard.php(51):
Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1,
true)
#26 /vagrant/www/planat-app/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23):
Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1,
true)
#27 /vagrant/www/planat-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(606):
Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#28 /vagrant/www/planat-app/public/index.php(49): Illuminate\Foundation\Application->run()

共有1个答案

干旺
2023-03-14

在构建api时,可以在全局范围内捕捉类似类型的异常。例如,如果用户试图获得一个ID不存在的客户,那么您可以这样做。

Customer::findOrFail($id);

然后,您可以在app/start/global.php中捕获所有此类型异常,如下所示。

App::error(function(ModelNotFoundException $modelNotFoundException){
    $errorResponse = [
        'errors'    => 'Not found any resource',
        'message'   => $modelNotFoundException->getMessage()
    ];

    return Response::json($errorResponse, 404);     //404 = Not found
});
 类似资料:
  • 因此,在这种情况下,我想配置Spring/Jackson返回{}(2字节)。 我已经考虑过在调用中返回new Object(),否则将返回void,但IMO这是一个肮脏的sout,必须有更好的东西。

  • 我试着像FOSRestBundleByExample/blob那样做。 我的行动: 不过,我有以下回应: 控制器必须返回一个响应(给定对象(FOS\RestBundle\View\View))。 我发现我可以在配置中将view\u response\u listener更改为true,但我有: 无法找到模板""。 有什么想法吗? 谢谢你,卡米尔

  • @ResponseBody无法返回forma json,我在浏览器上获得了Etat HTTP 406 我正在使用:jackson-core-asl 1.9.3和ajckson-mapper-asl 1.9.3 Getjson: Spring: 我的servelet-context: 我通过这个url进入浏览器: http://othman-pc.com:8080/dsi/natureparoper

  • 将响应实体主体类型更改为String非常有效。怎么了?也许这是窃听器? 附注:使用Spring4.2.8,Spring靴1.3.8。

  • 我有一个Spring控制器,它正在向我脚本中存在的ajax调用发送JSON响应。我在控制器方法中使用了@响应体,当通过ajax调用调用JSON时,它直接发送JSON作为响应。 在我添加JsonSanitizer之后。sanitize(myJsonString),它在ajax响应中作为html返回,而不是JSON。因此,我现在无法解析json对象。 示例代码: 在上面添加新行之后,响应以html而不

  • 我有一个ReST API到一个应用程序,该应用程序中的所有控制器都在下找到,这些控制器都返回JSON响应,并带有一个,它处理所有异常以映射到JSON格式的结果。 从Spring 4.0开始,这非常有效现在支持注释匹配。我无法解决的是如何为401-UnAuthated和400-Bad Request响应返回JSON结果。 相反,Spring只是将响应返回给容器(tomcat),该容器将其呈现为超文本