From 8a8fcb5cfc2a35a4e29c6daa2fd61a384c21ad7f Mon Sep 17 00:00:00 2001 From: Ivan Mitrikeski Date: Wed, 10 Jun 2026 19:21:00 -0400 Subject: [PATCH] Throw descriptive API exception messages --- lib/HttpRequestJson.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/HttpRequestJson.php b/lib/HttpRequestJson.php index 2d0d8c8..93b6137 100644 --- a/lib/HttpRequestJson.php +++ b/lib/HttpRequestJson.php @@ -208,10 +208,35 @@ protected static function processResponse($response) return ['location' => $lastHttpResponseHeaders['location']]; } - if (!in_array($httpCode, [null, $httpOK, $httpCreated, $httpDeleted]) || !empty($responseArray['error'])) { + if (!in_array($httpCode, [null, $httpOK, $httpCreated, $httpDeleted]) || !empty($responseArray['error'])|| !empty($responseArray['errors'])) { $message = "Request failed" . ($httpCode ? " with HTTP Code $httpCode" : "") . (!empty($responseArray['error']) ? ': ' . $responseArray['error'] : '.'); + + if (!empty($responseArray['error'])) { + $message = "Request failed" + . ($httpCode ? " with HTTP Code $httpCode" : "") + . (!empty($responseArray['error']) ? ': ' . $responseArray['error'] : '.'); + } elseif (!empty($responseArray['errors'])) { + $message = "Request failed" + . ($httpCode ? " with HTTP Code $httpCode: " : ": "); + + if (is_array($responseArray['errors'])) { + $parts = []; + + foreach ($responseArray['errors'] as $field => $messages) { + if (is_array($messages)) { + $parts[] = $field . ' - ' . implode(', ', $messages); + } else { + $parts[] = $field . ' - ' . $messages; + } + } + + $message .= implode('; ', $parts); + } else { + $message .= $responseArray['errors']; + } + } throw new ApiException($message, $httpCode); }