| @@ -32,20 +32,20 @@ | ||
| 32 | 32 | $category = RequestStatusTracker::recordWpError($response, $url); |
| 33 | 33 | $reason = RequestStatusTracker::describeCategoryFa($category); |
| 34 | 34 | |
| 35 | 35 | if ($category === 'blocked_http') { |
| 36 | - throw new BlockedHttpRequestException($reason . ' | جزئیات فنی: ' . $errorMessage); | |
| 36 | + throw new BlockedHttpRequestException(esc_html($reason . ' | جزئیات فنی: ' . $errorMessage)); | |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | if ($category === 'timeout') { |
| 40 | - throw RetryableException::apiTimeout($reason . ' | جزئیات فنی: ' . $errorMessage); | |
| 40 | + throw RetryableException::apiTimeout(esc_html($reason . ' | جزئیات فنی: ' . $errorMessage)); | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | if (in_array($category, ['dns_error', 'ssl_error', 'connection_error', 'network_error'], true)) { |
| 44 | - throw RetryableException::networkError($reason . ' | جزئیات فنی: ' . $errorMessage); | |
| 44 | + throw RetryableException::networkError(esc_html($reason . ' | جزئیات فنی: ' . $errorMessage)); | |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - throw RetryableException::temporary($reason . ' | جزئیات فنی: ' . $errorMessage); | |
| 47 | + throw RetryableException::temporary(esc_html($reason . ' | جزئیات فنی: ' . $errorMessage)); | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | private function handleHttpStatusCode(int $statusCode, $body, $url): array |
| 51 | 51 | { |
| @@ -51,9 +51,9 @@ | ||
| 51 | 51 | { |
| 52 | 52 | RequestStatusTracker::recordHttpStatus($statusCode, $url); |
| 53 | 53 | |
| 54 | 54 | if ($statusCode === 0) { |
| 55 | - throw new BlockedHttpRequestException(RequestStatusTracker::describeHttpStatusFa(0)); | |
| 55 | + throw new BlockedHttpRequestException(esc_html(RequestStatusTracker::describeHttpStatusFa(0))); | |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | if (in_array($statusCode, [200, 201, 202], true)) { |
| 59 | 59 | return $this->successResponse($body, $statusCode); |
| @@ -59,17 +59,17 @@ | ||
| 59 | 59 | return $this->successResponse($body, $statusCode); |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | if (in_array($statusCode, [408, 504], true)) { |
| 63 | - throw RetryableException::apiTimeout(RequestStatusTracker::describeHttpStatusFa($statusCode)); | |
| 63 | + throw RetryableException::apiTimeout(esc_html(RequestStatusTracker::describeHttpStatusFa($statusCode))); | |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | if ($statusCode === 429) { |
| 67 | - throw RetryableException::rateLimit(RequestStatusTracker::describeHttpStatusFa(429)); | |
| 67 | + throw RetryableException::rateLimit(esc_html(RequestStatusTracker::describeHttpStatusFa(429))); | |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | if (in_array($statusCode, [500, 502, 503], true)) { |
| 71 | - throw RetryableException::serverError(RequestStatusTracker::describeHttpStatusFa($statusCode)); | |
| 71 | + throw RetryableException::serverError(esc_html(RequestStatusTracker::describeHttpStatusFa($statusCode))); | |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | if ($statusCode === 401) { |
| 75 | 75 | if ($this->isBasalamDomain($url)) { |
| @@ -77,9 +77,9 @@ | ||
| 77 | 77 | SettingsConfig::TOKEN => null, |
| 78 | 78 | SettingsConfig::REFRESH_TOKEN => null, |
| 79 | 79 | ]); |
| 80 | 80 | } |
| 81 | - throw NonRetryableException::unauthorized(RequestStatusTracker::describeHttpStatusFa(401)); | |
| 81 | + throw NonRetryableException::unauthorized(esc_html(RequestStatusTracker::describeHttpStatusFa(401))); | |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | $clientErrors = [ |
| 85 | 85 | 400 => 'درخواست نامعتبر', |
| @@ -91,15 +91,28 @@ | ||
| 91 | 91 | if (isset($clientErrors[$statusCode])) { |
| 92 | 92 | $bodyMessage = $this->extractErrorMessageFromBody($body); |
| 93 | 93 | $base = $bodyMessage ?: $clientErrors[$statusCode]; |
| 94 | 94 | $reason = RequestStatusTracker::describeHttpStatusFa($statusCode); |
| 95 | - throw NonRetryableException::permanent($base . $reason); | |
| 95 | + throw NonRetryableException::permanent(esc_html($base . $reason)) | |
| 96 | + ->setResponseData($this->decodeBody($body)); | |
| 96 | 97 | } |
| 97 | 98 | |
| 98 | 99 | // Unknown error - treat as retryable |
| 99 | - throw RetryableException::temporary(RequestStatusTracker::describeHttpStatusFa($statusCode)); | |
| 100 | + throw RetryableException::temporary(esc_html(RequestStatusTracker::describeHttpStatusFa($statusCode))); | |
| 100 | 101 | } |
| 101 | 102 | |
| 103 | + private function decodeBody($body): ?array | |
| 104 | + { | |
| 105 | + if (is_array($body)) return $body; | |
| 106 | + | |
| 107 | + if (is_string($body) && $body !== '') { | |
| 108 | + $decoded = json_decode($body, true); | |
| 109 | + if (is_array($decoded)) return $decoded; | |
| 110 | + } | |
| 111 | + | |
| 112 | + return null; | |
| 113 | + } | |
| 114 | + | |
| 102 | 115 | private function extractErrorMessageFromBody($body): ?string |
| 103 | 116 | { |
| 104 | 117 | if (empty($body)) return null; |
| 105 | 118 | |
| @@ -141,7 +154,7 @@ | ||
| 141 | 154 | |
| 142 | 155 | public function handleTimeout(string $url): array |
| 143 | 156 | { |
| 144 | 157 | RequestStatusTracker::recordCategory('timeout', ['url' => $url]); |
| 145 | - throw RetryableException::apiTimeout(RequestStatusTracker::describeCategoryFa('timeout')); | |
| 158 | + throw RetryableException::apiTimeout(esc_html(RequestStatusTracker::describeCategoryFa('timeout'))); | |
| 146 | 159 | } |
| 147 | 160 | } |