Skip to content

Commit 973467a

Browse files
committed
Changed yii error format
This format is compatible with JSON:API, so it can be read by a standard JSON:API client properly.
1 parent a918611 commit 973467a

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/JsonApiErrorHandler.php

+36-12
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,48 @@ class JsonApiErrorHandler extends ErrorHandler
2626
/**
2727
* The way for format validation errors
2828
* 'yii' will return
29+
*
30+
* ```json
31+
* {
32+
* "errors": [
2933
* {
30-
* 'errors': [
31-
* { 'attributeName1': ['error message1', ...]},
32-
* { 'attributeName2': ['error message1', ...]},
33-
* ]
34-
* }
34+
* "status": 422,
35+
* "detail": "Model validation failed.",
36+
* "meta": {
37+
* "attributeName1": ["error message1", ...],
38+
* "attributeName2": ["error message1", ...],
39+
* }
40+
* }
41+
* ```
42+
*
3543
* 'attr' will return
44+
45+
* ```json
3646
* {
3747
* 'errors': [
3848
* { 'attribute': 'attributeName', 'message': 'errorMessage1'},
3949
* { 'attribute': 'attributeName', 'message': 'errorMessage2'},
4050
* { 'attribute': 'attributeName2', 'message': 'errorMessage'},
4151
* ]
4252
* }
53+
* ```
54+
*
4355
* 'spec' will return accordingly json api example
4456
* @see https://jsonapi.org/examples/#error-objects-multiple-errors
57+
*
58+
* ```json
4559
* {
4660
* 'errors': [
4761
* { 'status': '422', 'source': {'attribute': 'attributeName'}, 'detail': 'errorMessage1'},
4862
* { 'status': '422', 'source': {'attribute': 'attributeName'}, 'detail': 'errorMessage2'},
4963
* { 'status': '422', 'source': {'attribute': 'attributeName2'}, 'detail': 'errorMessage'},
5064
* ]
5165
* }
52-
**/
66+
* ```
67+
*/
5368
public $validationErrorFormat = self::ERROR_FORMAT_YII;
5469

70+
5571
protected function renderException($exception)
5672
{
5773
if (Yii::$app->has('response')) {
@@ -87,10 +103,7 @@ private function convertExceptionToJsonApi($exception)
87103
$exception = new HttpException(500, Yii::t('yii', 'An internal server error occurred.'));
88104
}
89105
if ($exception instanceof ValidationException) {
90-
return [
91-
'meta'=>['type'=>'Validation Errors'],
92-
'errors'=>$this->handleValidationErrors($exception->getErrors())
93-
];
106+
return $this->handleValidationErrors($exception->getErrors());
94107
}
95108
$error = new JsonApiError();
96109
if ($exception instanceof \Exception) {
@@ -121,7 +134,15 @@ private function convertExceptionToJsonApi($exception)
121134
private function handleValidationErrors(array $errors)
122135
{
123136
if ($this->validationErrorFormat === self::ERROR_FORMAT_YII) {
124-
return [$errors];
137+
return [
138+
'errors' => [
139+
[
140+
'status' => 422,
141+
'detail' => 'Model validation failed.',
142+
'meta' => $errors,
143+
],
144+
]
145+
];
125146
}
126147
$formattedErrors = [];
127148
foreach ($errors as $attr => $messages) {
@@ -130,6 +151,9 @@ private function handleValidationErrors(array $errors)
130151
$formattedErrors[] = $error;
131152
}
132153
}
133-
return $formattedErrors;
154+
return [
155+
'meta'=>['type'=>'Validation Errors'],
156+
'errors' => $formattedErrors,
157+
];
134158
}
135159
}

0 commit comments

Comments
 (0)