Browse Source

updated error code.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
46d9285640
  1. 19
      docs/guide/rest.md
  2. 8
      framework/rest/Controller.php
  3. 2
      framework/rest/Serializer.php

19
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

8
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.');
}
}

2
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[] = [

Loading…
Cancel
Save