Skip to content

Commit f9d0178

Browse files
authored
fix: devolver códigos de error como strings (#52)
* fix: return API error codes as strings * fix: only normalize numeric API error codes
1 parent 7af3bfa commit f9d0178

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

VERSION.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
4.7.1
2+
3+
## Fixed
4+
- Return numeric API error codes as strings.
5+
16
4.7.0
27

38
## Added

src/Exceptions/FacturapiException.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,22 @@ public function getRawBody(): ?string
4848
return $this->rawBody;
4949
}
5050

51-
public function getErrorCode(): mixed
51+
public function getErrorCode(): ?string
5252
{
53-
return is_array($this->errorData) ? ($this->errorData['code'] ?? null) : null;
53+
if (!is_array($this->errorData) || !array_key_exists('code', $this->errorData)) {
54+
return null;
55+
}
56+
57+
$code = $this->errorData['code'];
58+
if ($code === null) {
59+
return null;
60+
}
61+
62+
if (is_string($code)) {
63+
return $code;
64+
}
65+
66+
return is_int($code) || is_float($code) ? (string) $code : null;
5467
}
5568

5669
public function getErrorPath(): ?string

tests/Http/ErrorHandlingTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,20 @@ public function testNonJsonErrorsStillExposeRawBody(): void
8989
self::assertSame($rawBody, $exception->getRawBody());
9090
}
9191
}
92+
93+
public function testNumericApiErrorCodesAreConvertedToStrings(): void
94+
{
95+
$httpClient = new FakeHttpClient(
96+
new Response(400, ['Content-Type' => 'application/json'], '{"code": 400}')
97+
);
98+
99+
$invoices = new Invoices('sk_test_abc123', ['httpClient' => $httpClient]);
100+
101+
try {
102+
$invoices->create(['customer' => []]);
103+
self::fail('Expected FacturapiException to be thrown.');
104+
} catch (FacturapiException $exception) {
105+
self::assertSame('400', $exception->getErrorCode());
106+
}
107+
}
92108
}

0 commit comments

Comments
 (0)