From 46d9285640b4ac6300fb9ab9043e84a0872ac634 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 3 Mar 2014 13:45:55 -0500 Subject: [PATCH] updated error code. --- docs/guide/rest.md | 19 +++++++++++-------- framework/rest/Controller.php | 8 ++++---- framework/rest/Serializer.php | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/docs/guide/rest.md b/docs/guide/rest.md index 67c958c..192ad05 100644 --- a/docs/guide/rest.md +++ b/docs/guide/rest.md @@ -144,17 +144,20 @@ HTTP Status Code Summary ------------------------ * `200`: OK. Everything worked as expected. -* `201`: A data item was successfully created. Please check the `Location` header for the URL to access the new data item. -* `204`: A data item was successfully deleted. -* `304`: Data not modified. You can use cached data. -* `400`: Bad request. This could be caused by various reasons from the user side, such as invalid content type request, - invalid API version number, or data validation failure. If it is data validation failure, please check - the response body for error messages. +* `201`: A resource was successfully created in response to a `POST` request. The `Location` header + contains the URL pointing to the newly created resource. +* `204`: The request is handled successfully and the response contains no body content (like a `DELETE` request). +* `304`: Resource was not modified. You can use the cached version. +* `400`: Bad request. This could be caused by various reasons from the user side, such as invalid JSON + data in the request body, invalid action parameters, etc. * `401`: No valid API access token is provided. * `403`: The authenticated user is not allowed to access the specified API endpoint. -* `404`: The requested item does not exist. +* `404`: The requested resource does not exist. * `405`: Method not allowed. Please check the `Allow` header for allowed HTTP methods. -* `500`: Internal server error. +* `415`: Unsupported media type. The requested content type or version number is invalid. +* `422`: Data validation failed (in response to a `POST` request, for example). Please check the response body for detailed error messages. +* `429`: Too many requests. The request is rejected due to rate limiting. +* `500`: Internal server error. This could be caused by internal program errors. Data Formatting diff --git a/framework/rest/Controller.php b/framework/rest/Controller.php index 941fa57..7c93e62 100644 --- a/framework/rest/Controller.php +++ b/framework/rest/Controller.php @@ -8,9 +8,9 @@ namespace yii\rest; use Yii; -use yii\web\BadRequestHttpException; use yii\web\Response; use yii\web\UnauthorizedHttpException; +use yii\web\UnsupportedMediaTypeHttpException; use yii\web\VerbFilter; /** @@ -124,7 +124,7 @@ class Controller extends \yii\web\Controller /** * Resolves the response format and the API version number. - * @throws BadRequestHttpException + * @throws UnsupportedMediaTypeHttpException */ protected function resolveFormatAndVersion() { @@ -142,7 +142,7 @@ class Controller extends \yii\web\Controller if (in_array($params[self::HEADER_VERSION], $this->supportedVersions, true)) { $this->version = $params[self::HEADER_VERSION]; } else { - throw new BadRequestHttpException('You are requesting an invalid version number.'); + throw new UnsupportedMediaTypeHttpException('You are requesting an invalid version number.'); } } return; @@ -150,7 +150,7 @@ class Controller extends \yii\web\Controller } if (!isset($types['*/*'])) { - throw new BadRequestHttpException('None of your requested content types is valid.'); + throw new UnsupportedMediaTypeHttpException('None of your requested content types is supported.'); } } diff --git a/framework/rest/Serializer.php b/framework/rest/Serializer.php index ecacf2c..0cf777e 100644 --- a/framework/rest/Serializer.php +++ b/framework/rest/Serializer.php @@ -126,7 +126,7 @@ class Serializer extends Component */ protected function serializeModelErrors($model) { - $this->response->setStatusCode(400, 'Data Validation Failure'); + $this->response->setStatusCode(422, 'Data Validation Failed.'); $result = []; foreach ($model->getFirstErrors() as $name => $message) { $result[] = [