| @@ -9,8 +9,10 @@ | ||
| 9 | 9 | |
| 10 | 10 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 11 | 11 | use IvyForms\Common\Exceptions\NotFoundException; |
| 12 | 12 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 13 | +use IvyForms\Common\Exceptions\ServiceUnavailableException; | |
| 14 | +use IvyForms\Common\Exceptions\FieldValidationException; | |
| 13 | 15 | use IvyForms\Common\Exceptions\ValidationException; |
| 14 | 16 | use IvyForms\Common\Exceptions\ForbiddenException; |
| 15 | 17 | use IvyForms\Services\Translations\BackendStrings; |
| 16 | 18 | use RuntimeException; |
| @@ -67,8 +69,17 @@ | ||
| 67 | 69 | return new WP_REST_Response([ |
| 68 | 70 | 'message' => $e->getMessage(), |
| 69 | 71 | 'data' => [] |
| 70 | 72 | ], 404); |
| 73 | + } catch (ServiceUnavailableException $e) { | |
| 74 | + if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 75 | + error_log('Error 503: ' . $e->getMessage()); | |
| 76 | + } | |
| 77 | + | |
| 78 | + return new WP_REST_Response([ | |
| 79 | + 'message' => $e->getMessage(), | |
| 80 | + 'data' => [] | |
| 81 | + ], 503); | |
| 71 | 82 | } catch (InvalidArgumentException $e) { |
| 72 | 83 | // Log errors |
| 73 | 84 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 74 | 85 | error_log('Error: ' . $e->getMessage()); |
| @@ -85,11 +96,21 @@ | ||
| 85 | 96 | error_log('Validation error: ' . $e->getMessage()); |
| 86 | 97 | } |
| 87 | 98 | // Handle validation for data |
| 88 | 99 | // HTTP 422 Unprocessable Content |
| 100 | + $data = []; | |
| 101 | + $fieldErrors = $e->getFieldErrors(); | |
| 102 | + if ($fieldErrors !== []) { | |
| 103 | + if ($e instanceof FieldValidationException) { | |
| 104 | + $data['field_errors'] = $fieldErrors; | |
| 105 | + } else { | |
| 106 | + $data['fieldErrors'] = $fieldErrors; | |
| 107 | + } | |
| 108 | + } | |
| 109 | + | |
| 89 | 110 | return new WP_REST_Response([ |
| 90 | - 'message' => 'Validation error: ' . $e->getMessage(), | |
| 91 | - 'data' => [] | |
| 111 | + 'message' => $e->getMessage(), | |
| 112 | + 'data' => $data, | |
| 92 | 113 | ], 422); |
| 93 | 114 | } catch (QueryExecutionException $e) { |
| 94 | 115 | // Log errors |
| 95 | 116 | if (defined('WP_DEBUG') && WP_DEBUG) { |