| @@ -91,13 +91,26 @@ | ||
| 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(esc_html($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 | 100 | throw RetryableException::temporary(esc_html(RequestStatusTracker::describeHttpStatusFa($statusCode))); |
| 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; | |
| 100 | 113 | } |
| 101 | 114 | |
| 102 | 115 | private function extractErrorMessageFromBody($body): ?string |
| 103 | 116 | { |