@@ -26,32 +26,48 @@ class JsonApiErrorHandler extends ErrorHandler
26
26
/**
27
27
* The way for format validation errors
28
28
* 'yii' will return
29
+ *
30
+ * ```json
31
+ * {
32
+ * "errors": [
29
33
* {
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
+ *
35
43
* 'attr' will return
44
+
45
+ * ```json
36
46
* {
37
47
* 'errors': [
38
48
* { 'attribute': 'attributeName', 'message': 'errorMessage1'},
39
49
* { 'attribute': 'attributeName', 'message': 'errorMessage2'},
40
50
* { 'attribute': 'attributeName2', 'message': 'errorMessage'},
41
51
* ]
42
52
* }
53
+ * ```
54
+ *
43
55
* 'spec' will return accordingly json api example
44
56
* @see https://jsonapi.org/examples/#error-objects-multiple-errors
57
+ *
58
+ * ```json
45
59
* {
46
60
* 'errors': [
47
61
* { 'status': '422', 'source': {'attribute': 'attributeName'}, 'detail': 'errorMessage1'},
48
62
* { 'status': '422', 'source': {'attribute': 'attributeName'}, 'detail': 'errorMessage2'},
49
63
* { 'status': '422', 'source': {'attribute': 'attributeName2'}, 'detail': 'errorMessage'},
50
64
* ]
51
65
* }
52
- **/
66
+ * ```
67
+ */
53
68
public $ validationErrorFormat = self ::ERROR_FORMAT_YII ;
54
69
70
+
55
71
protected function renderException ($ exception )
56
72
{
57
73
if (Yii::$ app ->has ('response ' )) {
@@ -87,10 +103,7 @@ private function convertExceptionToJsonApi($exception)
87
103
$ exception = new HttpException (500 , Yii::t ('yii ' , 'An internal server error occurred. ' ));
88
104
}
89
105
if ($ exception instanceof ValidationException) {
90
- return [
91
- 'meta ' =>['type ' =>'Validation Errors ' ],
92
- 'errors ' =>$ this ->handleValidationErrors ($ exception ->getErrors ())
93
- ];
106
+ return $ this ->handleValidationErrors ($ exception ->getErrors ());
94
107
}
95
108
$ error = new JsonApiError ();
96
109
if ($ exception instanceof \Exception) {
@@ -121,7 +134,15 @@ private function convertExceptionToJsonApi($exception)
121
134
private function handleValidationErrors (array $ errors )
122
135
{
123
136
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
+ ];
125
146
}
126
147
$ formattedErrors = [];
127
148
foreach ($ errors as $ attr => $ messages ) {
@@ -130,6 +151,9 @@ private function handleValidationErrors(array $errors)
130
151
$ formattedErrors [] = $ error ;
131
152
}
132
153
}
133
- return $ formattedErrors ;
154
+ return [
155
+ 'meta ' =>['type ' =>'Validation Errors ' ],
156
+ 'errors ' => $ formattedErrors ,
157
+ ];
134
158
}
135
159
}
0 commit comments