Commit 46d92856 by Qiang Xue

updated error code.

parent 7a04dbaa
...@@ -144,17 +144,20 @@ HTTP Status Code Summary ...@@ -144,17 +144,20 @@ HTTP Status Code Summary
------------------------ ------------------------
* `200`: OK. Everything worked as expected. * `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. * `201`: A resource was successfully created in response to a `POST` request. The `Location` header
* `204`: A data item was successfully deleted. contains the URL pointing to the newly created resource.
* `304`: Data not modified. You can use cached data. * `204`: The request is handled successfully and the response contains no body content (like a `DELETE` request).
* `400`: Bad request. This could be caused by various reasons from the user side, such as invalid content type request, * `304`: Resource was not modified. You can use the cached version.
invalid API version number, or data validation failure. If it is data validation failure, please check * `400`: Bad request. This could be caused by various reasons from the user side, such as invalid JSON
the response body for error messages. data in the request body, invalid action parameters, etc.
* `401`: No valid API access token is provided. * `401`: No valid API access token is provided.
* `403`: The authenticated user is not allowed to access the specified API endpoint. * `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. * `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 Data Formatting
......
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
namespace yii\rest; namespace yii\rest;
use Yii; use Yii;
use yii\web\BadRequestHttpException;
use yii\web\Response; use yii\web\Response;
use yii\web\UnauthorizedHttpException; use yii\web\UnauthorizedHttpException;
use yii\web\UnsupportedMediaTypeHttpException;
use yii\web\VerbFilter; use yii\web\VerbFilter;
/** /**
...@@ -124,7 +124,7 @@ class Controller extends \yii\web\Controller ...@@ -124,7 +124,7 @@ class Controller extends \yii\web\Controller
/** /**
* Resolves the response format and the API version number. * Resolves the response format and the API version number.
* @throws BadRequestHttpException * @throws UnsupportedMediaTypeHttpException
*/ */
protected function resolveFormatAndVersion() protected function resolveFormatAndVersion()
{ {
...@@ -142,7 +142,7 @@ class Controller extends \yii\web\Controller ...@@ -142,7 +142,7 @@ class Controller extends \yii\web\Controller
if (in_array($params[self::HEADER_VERSION], $this->supportedVersions, true)) { if (in_array($params[self::HEADER_VERSION], $this->supportedVersions, true)) {
$this->version = $params[self::HEADER_VERSION]; $this->version = $params[self::HEADER_VERSION];
} else { } else {
throw new BadRequestHttpException('You are requesting an invalid version number.'); throw new UnsupportedMediaTypeHttpException('You are requesting an invalid version number.');
} }
} }
return; return;
...@@ -150,7 +150,7 @@ class Controller extends \yii\web\Controller ...@@ -150,7 +150,7 @@ class Controller extends \yii\web\Controller
} }
if (!isset($types['*/*'])) { 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.');
} }
} }
......
...@@ -126,7 +126,7 @@ class Serializer extends Component ...@@ -126,7 +126,7 @@ class Serializer extends Component
*/ */
protected function serializeModelErrors($model) protected function serializeModelErrors($model)
{ {
$this->response->setStatusCode(400, 'Data Validation Failure'); $this->response->setStatusCode(422, 'Data Validation Failed.');
$result = []; $result = [];
foreach ($model->getFirstErrors() as $name => $message) { foreach ($model->getFirstErrors() as $name => $message) {
$result[] = [ $result[] = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment