Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
b1e7c753
Commit
b1e7c753
authored
Sep 07, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #2702: added doc about customizing error response format [skip ci]
parent
9885aac0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
2 deletions
+53
-2
rest-error-handling.md
docs/guide/rest-error-handling.md
+52
-1
Response.php
framework/web/Response.php
+1
-1
No files found.
docs/guide/rest-error-handling.md
View file @
b1e7c753
...
@@ -17,7 +17,6 @@ Transfer-Encoding: chunked
...
@@ -17,7 +17,6 @@ Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
Content-Type: application/json; charset=UTF-8
{
{
"type": "yii\\web\\NotFoundHttpException",
"name": "Not Found Exception",
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"message": "The requested resource was not found.",
"code": 0,
"code": 0,
...
@@ -42,3 +41,55 @@ The following list summarizes the HTTP status code that are used by the Yii REST
...
@@ -42,3 +41,55 @@ The following list summarizes the HTTP status code that are used by the Yii REST
*
`422`
: Data validation failed (in response to a
`POST`
request, for example). Please check the response body for detailed error messages.
*
`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 was rejected due to rate limiting.
*
`429`
: Too many requests. The request was rejected due to rate limiting.
*
`500`
: Internal server error. This could be caused by internal program errors.
*
`500`
: Internal server error. This could be caused by internal program errors.
## Customizing Error Response
Sometimes you may want to customize the default error response format. For example, instead of relying on
using different HTTP statuses to indicate different errors, you would like to always use 200 as HTTP status
and enclose the actual HTTP status code as part of the JSON structure in the response, like shown in the following,
```
HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"success": false,
"data": {
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
}
```
To achieve this goal, you can respond to the
`beforeSend`
event of the
`response`
component in the application configuration:
```
php
return
[
// ...
'components'
=>
[
'response'
=>
[
'class'
=>
'yii\web\Response'
,
'on beforeSend'
=>
function
(
$event
)
{
$response
=
$event
->
sender
;
if
(
$response
->
data
!==
null
&&
!
empty
(
Yii
::
$app
->
request
->
get
[
'suppress_response_code'
]))
{
$response
->
data
=
[
'success'
=>
$response
->
isSuccessful
,
'data'
=>
$response
->
data
,
];
$response
->
statusCode
=
200
;
}
},
],
],
];
```
The above code will reformat the response (for both successful and failed responses) as explained when
`suppress_response_code`
is passed as a
`GET`
parameter.
framework/web/Response.php
View file @
b1e7c753
...
@@ -345,7 +345,7 @@ class Response extends \yii\base\Response
...
@@ -345,7 +345,7 @@ class Response extends \yii\base\Response
$headers
=
$this
->
getHeaders
();
$headers
=
$this
->
getHeaders
();
foreach
(
$headers
as
$name
=>
$values
)
{
foreach
(
$headers
as
$name
=>
$values
)
{
$name
=
str_replace
(
' '
,
'-'
,
ucwords
(
str_replace
(
'-'
,
' '
,
$name
)));
$name
=
str_replace
(
' '
,
'-'
,
ucwords
(
str_replace
(
'-'
,
' '
,
$name
)));
// set replace for first occur
a
nce of header but false afterwards to allow multiple
// set replace for first occur
re
nce of header but false afterwards to allow multiple
$replace
=
true
;
$replace
=
true
;
foreach
(
$values
as
$value
)
{
foreach
(
$values
as
$value
)
{
header
(
"
$name
:
$value
"
,
$replace
);
header
(
"
$name
:
$value
"
,
$replace
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment