> For the complete documentation index, see [llms.txt](/llms.txt)

# Errors

A failed request throws a typed `ApiError` carrying the full context — status, method,
path, response body, headers, and the request id:

```ts
import { Client, ApiError } from '@your-org/petstore';

try {
  await client.pets.createPet({ name: '' });
} catch (err) {
  if (err instanceof ApiError) {
    console.error(err.status);     // e.g. 422
    console.error(err.requestId);  // for support
    console.error(err.body);       // parsed response body
  }
}
```

The error message is `"<method> <path> -> <status>: <body>"`, so it's readable in logs
without unwrapping. Subclasses per status family are generated when the spec models them.

## Every SDK, same error model

The same catch-and-narrow shape is emitted in every language — these are the bytes
`glotto generate` produced, each stamped with the checksum of the artifact it came from:

**TypeScript** (regenerated + byte-diffed in CI `a0f8af4b462c`)

```ts
import { ApiError } from 'glotto-sdk';

try {
  // any client call
} catch (err) {
  if (err instanceof ApiError) {
    console.error(err.status, err.toString());
  }
}
```

**React Native** (regenerated + byte-diffed in CI `a1fefe7d3458`)

```ts
import { ApiError } from 'glotto-sdk';

try {
  // any client call
} catch (err) {
  if (err instanceof ApiError) {
    console.error(err.status, err.toString());
  }
}
```

**Python** (regenerated + byte-diffed in CI `2242160f8958`)

```python
from glotto_sdk import ApiError

try:
    ...  # any client call
except ApiError as err:
    print(err.status, err)
```

**Go** (regenerated + byte-diffed in CI `f0c565cc46f8`)

```go
// err from any client call
var apiErr *sdk.APIError
if errors.As(err, &apiErr) {
    fmt.Println(apiErr.Status, apiErr.Error())
}
```

**Java** (regenerated + byte-diffed in CI `12c05e098737`)

```java
import com.glotto.Client;
import com.glotto.errors.ApiError;

public class Snippet {
    public static void main(String[] args) throws Exception {
        try {
            // any client call
        } catch (ApiError e) {
            System.err.println(e.status() + ": " + e.getMessage());
        }
    }
}
```

**Kotlin** (regenerated + byte-diffed in CI `0f5e53ae2522`)

```kotlin
import com.glotto.errors.ApiError

try {
    // any client call
} catch (e: ApiError) {
    println("${e.status}: ${e.message}")
}
```

**C#** (regenerated + byte-diffed in CI `c5013ce0e88b`)

```csharp
using Glotto;
using ApiError = Glotto.Errors.ApiError;

try
{
    // any client call
}
catch (ApiError err)
{
    Console.WriteLine($"{err.Status}: {err.Message}");
}
```

**PHP** (regenerated + byte-diffed in CI `1acd16081b46`)

```php
<?php
require 'vendor/autoload.php';

try {
    // any client call
} catch (Glotto\Errors\ApiError $e) {
    fwrite(STDERR, $e->status . ': ' . $e->getMessage() . PHP_EOL);
}
```

**Ruby** (regenerated + byte-diffed in CI `a2b854f6593f`)

```ruby
require 'glotto'

begin
  # any client call
rescue Glotto::ApiError => e
  warn "#{e.status}: #{e.message}"
end
```

**Rust** (regenerated + byte-diffed in CI `716e9ec181d1`)

```rust
// `result` is the Result<_, ApiError> returned by any client call
if let Err(err) = result {
    eprintln!("{err}");
}
```

**Swift** (regenerated + byte-diffed in CI `0cc357352baf`)

```swift
import GlottoSdk

do {
    // any client call
} catch let error as APIError {
    print(error.status, error.kind)
}
```

**Dart** (regenerated + byte-diffed in CI `67567d6df435`)

```dart
try {
  // any client call
} on ApiError catch (e) {
  print('API error ${e.status}');
}
```

**Elixir** (regenerated + byte-diffed in CI `b97af402f1d7`)

```elixir
# any generated operation, e.g. Glotto.<Resource>.<operation>(client, ...)
case client_call do
  {:ok, result} ->
    result

  {:error, %Glotto.ApiError{status: status} = error} ->
    IO.warn("#{status}: #{Exception.message(error)}")
end
```
