Skip to content
Closed
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
19 changes: 19 additions & 0 deletions README.es.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ try {
- `Facturapi\\Exceptions\\Facturapi_Exception` está deprecada en v4 y se eliminará en v5.
- Usa `Facturapi\\Exceptions\\FacturapiException`.

## Idioma de los errores de la API

En API V2 puedes solicitar mensajes de error en inglés con `Accept-Language`. Usa la configuración existente del cliente:

```php
use Facturapi\Facturapi;
use GuzzleHttp\Client;

$facturapi = new Facturapi('YOUR_API_KEY', [
'httpClient' => new Client([
'headers' => ['Accept-Language' => 'en'],
'timeout' => 360,
'connect_timeout' => 3,
]),
]);
```

Usa `es` para español. También se aceptan variantes como `en-US` y preferencias como `en;q=0.9, es;q=0.5`. Sin el header o sin un idioma compatible, la API usa español. La preferencia se aplica a todas las solicitudes de esta instancia; usa instancias separadas si necesitas varios idiomas. Los códigos no cambian y los mensajes externos SAT/PAC y los mensajes legacy no catalogados conservan su idioma original. La localización depende del soporte de API V2 en el servidor; no traduce errores locales del SDK.

## Documentación 📚

Documentación completa: [https://docs.facturapi.io](https://docs.facturapi.io)
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ try {
- `Facturapi\\Exceptions\\Facturapi_Exception` is deprecated in v4 and will be removed in v5.
- Use `Facturapi\\Exceptions\\FacturapiException`.

## API error language

For API V2, request English error messages with `Accept-Language` using the existing client configuration:

```php
use Facturapi\Facturapi;
use GuzzleHttp\Client;

$facturapi = new Facturapi('YOUR_API_KEY', [
'httpClient' => new Client([
'headers' => ['Accept-Language' => 'en'],
'timeout' => 360,
'connect_timeout' => 3,
]),
]);
```

Use `es` for Spanish. Regional variants such as `en-US` and preferences such as `en;q=0.9, es;q=0.5` are also supported. Missing or unsupported language preferences fall back to Spanish. The setting applies to all requests from this instance; use separate instances for different languages. Codes remain unchanged, and external SAT/PAC messages and uncataloged legacy messages retain their original language. Localization requires server-side API V2 support; it does not translate local SDK errors.

## Documentation 📚

Full docs: [https://docs.facturapi.io](https://docs.facturapi.io)
Expand Down
36 changes: 36 additions & 0 deletions tests/Http/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace Facturapi\Tests\Http;

use Facturapi\Facturapi;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Facturapi\Exceptions\FacturapiException;
use Facturapi\Resources\Invoices;
use Facturapi\Tests\Support\FakeHttpClient;
Expand All @@ -12,6 +17,37 @@

final class ErrorHandlingTest extends TestCase
{
public function testGuzzleDefaultLanguageHeaderIsSentWithSdkAuthentication(): void
{
$history = [];
$stack = HandlerStack::create(new MockHandler([
new Response(401, ['Content-Type' => 'application/json'], json_encode([
'message' => 'The provided API key is invalid.',
'code' => 'api_key_invalid',
'status' => 401,
])),
]));
$stack->push(Middleware::history($history));
$facturapi = new Facturapi('sk_test_123', [
'httpClient' => new Client([
'handler' => $stack,
'headers' => ['Accept-Language' => 'en-US'],
'timeout' => 360,
'connect_timeout' => 3,
]),
]);

try {
$facturapi->Invoices->retrieve('inv_123');
self::fail('Expected FacturapiException');
} catch (FacturapiException $exception) {
self::assertSame('The provided API key is invalid.', $exception->getMessage());
self::assertSame('api_key_invalid', $exception->getErrorCode());
}
self::assertSame('en-US', $history[0]['request']->getHeaderLine('Accept-Language'));
self::assertSame('Basic ' . base64_encode('sk_test_123:'), $history[0]['request']->getHeaderLine('Authorization'));
}

public function testApiErrorShapeIsFullyAvailableOnException(): void
{
$errorBody = [
Expand Down
Loading