您好,欢迎访问一九零五行业门户网

Laravel & Lumen RESTFul API 扩展包:Dingo API(四) -- 错误和异常响应

在构建api的时候处理错误是一件痛苦的事儿,在dingo api中,你不需要手动构建错误响应,只需要抛出一个继承自 symfony\component\httpkernel\exception\httpexception的异常,api会自动为你处理这个响应。
下面是dingo api内置的symfony异常:
异常 状态码
symfony\component\httpkernel\exception\accessdeniedhttpexception 403
symfony\component\httpkernel\exception\badrequesthttpexception 400
symfony\component\httpkernel\exception\conflicthttpexception 409
symfony\component\httpkernel\exception\gonehttpexception 410
symfony\component\httpkernel\exception\httpexception 500
symfony\component\httpkernel\exception\lengthrequiredhttpexception 411
symfony\component\httpkernel\exception\methodnotallowedhttpexception 405
symfony\component\httpkernel\exception\notacceptablehttpexception 406
symfony\component\httpkernel\exception\notfoundhttpexception 404
symfony\component\httpkernel\exception\preconditionfailedhttpexception 412
symfony\component\httpkernel\exception\preconditionrequiredhttpexception 428
symfony\component\httpkernel\exception\serviceunavailablehttpexception 503
symfony\component\httpkernel\exception\toomanyrequestshttpexception 429
symfony\component\httpkernel\exception\unauthorizedhttpexception 401
symfony\component\httpkernel\exception\unsupportedmediatypehttpexception 415
下面是一个示例,当我们尝试更新一条已经被别人更新过的记录时抛出一个 conflicthttpexception异常:
$api->version('v1', function ($api) { $api->put('user/{id}', function ($id) { $user = user::find($id); if ($user->updated_at > app('request')->get('last_updated')) { throw new symfony\component\httpkernel\exception\conflicthttpexception('user was updated prior to your request.'); } // no error, we can continue to update the user as per usual. });});
dingo api会自动捕获抛出的异常并将其转化为json格式,响应的http状态码也会相应更改以匹配这个异常, conflicthttpexception对应的http状态码是 409,默认的json格式错误信息如下:
{ message: user was updated prior to your request., status_code: 409}
1、资源异常 以下是资源异常,每个异常都会返回 422状态码:
dingo\api\exception\deleteresourcefailedexceptiondingo\api\exception\resourceexceptiondingo\api\exception\storeresourcefailedexceptiondingo\api\exception\updateresourcefailedexception
这些异常特殊之处在于你可以将创建、更新或者删除资源时遇到的验证错误传递到这些异常中。
下面我们就来看一个创建新用户验证失败抛出 storeresourcefailedexception异常的例子:
$api->version('v1', function ($api) { $api->post('users', function () { $rules = [ 'username' => ['required', 'alpha'], 'password' => ['required', 'min:7'] ]; $payload = app('request')->only('username', 'password'); $validator = app('validator')->make($payload, $rules); if ($validator->fails()) { throw new dingo\api\exception\storeresourcefailedexception('could not create new user.', $validator->errors()); } // create user as per usual. });});
dingo api会自动捕获抛出的异常并将其转化为json格式,响应的http状态码也会更改为与异常相匹配的值,资源异常会返回 422状态码以及如下json格式错误信息:
{ message: could not create new user., status_code: 422, errors: { username: [ the username field is required. ], password: [ the password field is required. ] }}
2、自定义http异常 你可以创建自定义的http异常,前提是它们继承自 symfony\component\httpkernel\exception\httpexception或者实现了 symfony\component\httpkernel\exception\httpexceptioninterface接口。
3、自定义异常响应 如果你需要自定义异常返回的响应可以注册一个异常处理器:
app('dingo\api\exception\handler')->register(function (symfony\component\httpkernel\exception\unauthorizedhttpexception $exception) { return response::make(['error' => 'hey, what do you think you are doing!?'], 401);});
现在如果认证失败我们会显示如下json格式信息:
{ error: hey, what do you think you are doing!?}
4、表单请求 如果你使用表单请求,那么需要继承api表单请求基类或者实现自己的类。api请求基类会检查输入请求是否是请求api,如果是的话当验证失败会抛出 dingo\api\exception\validationhttpexception异常。这个异常会被dingo api渲染并返回错误响应。
如果你想要实现自己的表单请求,则必须重载 failedvalidation和 failedauthorization方法,这些方法必须抛出上述其中一种异常而不是laravel抛出的http异常。
其它类似信息

推荐信息