Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion VERSION.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
4.5.0
4.5.1

## Added
- Expose structured API error metadata on `FacturapiException`, including `code`, `path`, `location`, `errors`, `logId`, and response headers.

## Fixed
- Return API error codes as strings, including legacy numeric codes.
17 changes: 15 additions & 2 deletions src/Exceptions/FacturapiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,22 @@ public function getRawBody(): ?string
return $this->rawBody;
}

public function getErrorCode(): mixed
public function getErrorCode(): ?string
{
return is_array($this->errorData) ? ($this->errorData['code'] ?? null) : null;
if (!is_array($this->errorData) || !array_key_exists('code', $this->errorData)) {
return null;
}

$code = $this->errorData['code'];
if ($code === null) {
return null;
}

if (is_string($code)) {
return $code;
}

return is_int($code) || is_float($code) ? (string) $code : null;
}

public function getErrorPath(): ?string
Expand Down
16 changes: 16 additions & 0 deletions tests/Http/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,20 @@ public function testNonJsonErrorsStillExposeRawBody(): void
self::assertSame($rawBody, $exception->getRawBody());
}
}

public function testNumericApiErrorCodesAreConvertedToStrings(): void
{
$httpClient = new FakeHttpClient(
new Response(400, ['Content-Type' => 'application/json'], '{"code": 400}')
);

$invoices = new Invoices('sk_test_abc123', ['httpClient' => $httpClient]);

try {
$invoices->create(['customer' => []]);
self::fail('Expected FacturapiException to be thrown.');
} catch (FacturapiException $exception) {
self::assertSame('400', $exception->getErrorCode());
}
}
}
Loading