Skip to content

Commit 739371b

Browse files
committed
fix(http): normalize non-JSON gateway responses as invalid-response network errors
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent ce60502 commit 739371b

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

src/Http/NfseClient.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,17 @@ private function fetchAndDecode(string $path, mixed $context): array
274274
throw new NetworkException('Failed to connect to SEFIN gateway at ' . $url);
275275
}
276276

277-
$decoded = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
277+
try {
278+
$decoded = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
279+
} catch (\JsonException $e) {
280+
$responsePreview = trim(substr(strip_tags($body), 0, 180));
281+
282+
throw new NetworkException(
283+
'Unexpected non-JSON response from SEFIN gateway' . ($responsePreview !== '' ? ': ' . $responsePreview : ''),
284+
NfseErrorCode::InvalidResponse,
285+
$e,
286+
);
287+
}
278288

279289
if (!is_array($decoded)) {
280290
throw new NetworkException(

0 commit comments

Comments
 (0)