| @@ -83,11 +83,14 @@ | ||
| 83 | 83 | * 'data' => Optional. Error data. Default empty string. |
| 84 | 84 | * ], |
| 85 | 85 | * string |
| 86 | 86 | * ] |
| 87 | + * @param int|null $status HTTP status the framework's wpErrorToResponse should use | |
| 88 | + * (Route::dispatch converts any returned WP_Error via rest_convert_error_to_response, | |
| 89 | + * which reads error_data['status'] and falls back to 500 when absent). | |
| 87 | 90 | * @return WP_Error |
| 88 | 91 | */ |
| 89 | - public static function makeErrorResponse($errors): WP_Error | |
| 92 | + public static function makeErrorResponse($errors, ?int $status = null): WP_Error | |
| 90 | 93 | { |
| 91 | 94 | $wpError = new WP_Error(); |
| 92 | 95 | |
| 93 | 96 | if (is_array($errors)) { |
| @@ -92,19 +95,23 @@ | ||
| 92 | 95 | |
| 93 | 96 | if (is_array($errors)) { |
| 94 | 97 | foreach ($errors as $key => $error) { |
| 95 | 98 | if (is_array($error)) { |
| 99 | + $data = Arr::get($error, 'data', ''); | |
| 100 | + if ($status) { | |
| 101 | + $data = is_array($data) ? array_merge($data, ['status' => $status]) : ['status' => $status]; | |
| 102 | + } | |
| 96 | 103 | $wpError->add( |
| 97 | 104 | Arr::get($error, 'code', $key), |
| 98 | 105 | Arr::get($error, 'message'), |
| 99 | - Arr::get($error, 'data', '') | |
| 106 | + $data | |
| 100 | 107 | ); |
| 101 | 108 | } else { |
| 102 | - $wpError->add($key, $error); | |
| 109 | + $wpError->add($key, $error, $status ? ['status' => $status] : ''); | |
| 103 | 110 | } |
| 104 | 111 | } |
| 105 | 112 | } else { |
| 106 | - $wpError->add($errors, $errors); | |
| 113 | + $wpError->add($errors, $errors, $status ? ['status' => $status] : ''); | |
| 107 | 114 | } |
| 108 | 115 | return $wpError; |
| 109 | 116 | } |
| 110 | 117 | |