在dingo Api 中,不需要手动的指定错误,只需要抛出一个继承Symfony\Component\HttpKernel\Exception\HttpException
的异常,API会自动处理这个响应。
异常 | 状态码 |
---|---|
Symfony\Component\HttpKernel\Exception\BadRequestHttpException | 400 |
Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException | 401 |
Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException | 403 |
Symfony\Component\HttpKernel\Exception\NotFoundHttpException | 404 |
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException | 405 |
Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException | 406 |
Symfony\Component\HttpKernel\Exception\ConflictHttpException | 409 |
Symfony\Component\HttpKernel\Exception\GoneHttpException | 410 |
Symfony\Component\HttpKernel\Exception\LengthRequiredHttpException | 411 |
Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException | 412 |
Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException | 415 |
Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException | 428 |
Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException | 429 |
Symfony\Component\HttpKernel\Exception\HttpException | 500 |
Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException | 503 |
列子:
$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.
});
});
当程序出现上面的异常,会自动捕捉到这个异常并转化成json,同时http响应吗也会自动变成这个异常的status_code以相对应
{
"message": "User was updated prior to your request.",
"status_code": 409
}
资源异常
下面列表中是常见的资源异常,resource exceptions, 他们返回的状态吗全部是422
Dingo\Api\Exception\DeleteResourceFailedException
Dingo\Api\Exception\ResourceException
Dingo\Api\Exception\StoreResourceFailedException
Dingo\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 会自动捕捉这个异常并且将其转成Json格式,同时HTTP Status也会变成status_code,
{
"message": "Could not create new user.",
"errors": {
"username": [
"The username field is required."
],
"password": [
"The password field is required."
]
},
"status_code": 422
}
用户自定义的HTTP异常
创建自己的HTTP 异常,需要继承Symfony\Component\HttpKernel\Exception\HttpException或者是实现Symfony\Component\HttpKernel\Exception\HttpExceptionInterface接口
自定义异常响应
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);
}); ????????????
如果你使用表单请求,那么需要继承API表单请求基类或者实现自己的类。API请求基类会检查输入请求是否是请求API,如果是的话当验证失败会抛出Dingo\Api\Exception\ValidationHttpException异常。这个异常会被Dingo API渲染并返回错误响应。
如果你想要实现自己的表单请求,则必须重载failedValidation和failedAuthorization方法,这些方法必须抛出上述其中一种异常而不是Laravel抛出的HTTP异常。