| @@ -1,23 +1,21 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * @copyright © TMS-Plugins. All rights reserved. | |
| 4 | + * @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | 5 | * @licence See COPYING.md for license details. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace IvyForms\Controllers; |
| 9 | 9 | |
| 10 | -// phpcs:disable PSR1.Files.SideEffects | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; // Exit if accessed directly | |
| 13 | -} | |
| 14 | - | |
| 15 | 10 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 16 | 11 | use IvyForms\Common\Exceptions\NotFoundException; |
| 17 | 12 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 13 | +use IvyForms\Common\Exceptions\ServiceUnavailableException; | |
| 14 | +use IvyForms\Common\Exceptions\FieldValidationException; | |
| 18 | 15 | use IvyForms\Common\Exceptions\ValidationException; |
| 19 | 16 | use IvyForms\Common\Exceptions\ForbiddenException; |
| 17 | +use IvyForms\Services\Translations\BackendStrings; | |
| 20 | 18 | use RuntimeException; |
| 21 | 19 | use WP_REST_Request; |
| 22 | 20 | use WP_REST_Response; |
| 23 | 21 | |
| @@ -40,13 +38,15 @@ | ||
| 40 | 38 | try { |
| 41 | 39 | $response = $this->handle($data); |
| 42 | 40 | |
| 43 | 41 | if ($response->get_status() !== 200) { |
| 44 | - throw new RuntimeException("Unexpected HTTP status code: {$response->get_status()}"); | |
| 42 | + throw new RuntimeException( | |
| 43 | + BackendStrings::getExceptionStrings()['unexpected_http_status'] . ": {$response->get_status()}" | |
| 44 | + ); | |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | return new WP_REST_Response([ |
| 48 | - 'message' => 'OK', | |
| 48 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 49 | 49 | 'data' => $response->get_data(), |
| 50 | 50 | ], 200); |
| 51 | 51 | } catch (ForbiddenException $e) { |
| 52 | 52 | // Log errors |
| @@ -69,8 +69,17 @@ | ||
| 69 | 69 | return new WP_REST_Response([ |
| 70 | 70 | 'message' => $e->getMessage(), |
| 71 | 71 | 'data' => [] |
| 72 | 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); | |
| 73 | 82 | } catch (InvalidArgumentException $e) { |
| 74 | 83 | // Log errors |
| 75 | 84 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 76 | 85 | error_log('Error: ' . $e->getMessage()); |
| @@ -87,11 +96,21 @@ | ||
| 87 | 96 | error_log('Validation error: ' . $e->getMessage()); |
| 88 | 97 | } |
| 89 | 98 | // Handle validation for data |
| 90 | 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 | + | |
| 91 | 110 | return new WP_REST_Response([ |
| 92 | - 'message' => 'Validation error: ' . $e->getMessage(), | |
| 93 | - 'data' => [] | |
| 111 | + 'message' => $e->getMessage(), | |
| 112 | + 'data' => $data, | |
| 94 | 113 | ], 422); |
| 95 | 114 | } catch (QueryExecutionException $e) { |
| 96 | 115 | // Log errors |
| 97 | 116 | if (defined('WP_DEBUG') && WP_DEBUG) { |