> ## Documentation Index
> Fetch the complete documentation index at: https://shinydapps-bd9fa40b.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Referência de Erros

> Códigos de erro semânticos do l402-kit para agentes e desenvolvedores.

Todos os erros do l402-kit seguem um formato JSON estruturado para que agentes possam analisá-los e agir sobre eles de forma programática.

```json theme={null}
{
  "error": "ERROR_CODE",
  "code": 402,
  "cause": "Human-readable explanation",
  "action": "recommended_action",
  "retry": false
}
```

***

## Códigos de erro

### PAYMENT\_FAILED

```json theme={null}
{
  "error": "PAYMENT_FAILED",
  "code": 402,
  "cause": "Insufficient balance in wallet",
  "action": "increase_budget",
  "retry": false
}
```

A carteira não conseguiu pagar a fatura. Verifique o saldo e o limite de `budgetSats`.

***

### TOKEN\_EXPIRED

```json theme={null}
{
  "error": "TOKEN_EXPIRED",
  "code": 402,
  "cause": "Macaroon expiry timestamp exceeded",
  "action": "request_new_invoice",
  "retry": true
}
```

O token L402 foi gerado, mas não foi utilizado antes do seu TTL (padrão: 1 hora). Tente novamente a requisição para obter uma nova fatura.

***

### BUDGET\_EXCEEDED

```json theme={null}
{
  "error": "BUDGET_EXCEEDED",
  "code": 402,
  "cause": "Session budget of {n} sats exhausted",
  "action": "stop_or_increase_budget",
  "retry": false
}
```

O agente gastou mais do que `budgetSats` na sessão atual. Aumente o orçamento ou encerre a sessão.

***

### INVALID\_MACAROON

```json theme={null}
{
  "error": "INVALID_MACAROON",
  "code": 402,
  "cause": "Macaroon is not valid base64 JSON",
  "action": "request_new_invoice",
  "retry": true
}
```

O macaroon não pôde ser decodificado. Geralmente causado por um token corrompido. Tente novamente do início.

***

### TOKEN\_ALREADY\_USED

```json theme={null}
{
  "error": "TOKEN_ALREADY_USED",
  "code": 401,
  "cause": "Preimage was already presented — replay attack blocked",
  "action": "do_not_retry",
  "retry": false
}
```

Proteção contra replay ativada. Cada preimage só pode ser utilizado uma vez. Nunca reutilize tokens.

***

### NODE\_UNREACHABLE

```json theme={null}
{
  "error": "NODE_UNREACHABLE",
  "code": 503,
  "cause": "Lightning node did not respond within timeout",
  "action": "retry_with_backoff",
  "retry": true
}
```

O provedor Lightning está inacessível. Tente novamente com backoff exponencial.

***

### INVOICE\_EXPIRED

```json theme={null}
{
  "error": "INVOICE_EXPIRED",
  "code": 402,
  "cause": "BOLT11 invoice expired before payment was submitted",
  "action": "request_new_invoice",
  "retry": true
}
```

Faturas BOLT11 expiram (padrão: 600 segundos). Tente novamente a requisição original para receber uma nova fatura.

***

### WRONG\_PREIMAGE

```json theme={null}
{
  "error": "WRONG_PREIMAGE",
  "code": 402,
  "cause": "SHA256(preimage) does not match paymentHash in macaroon",
  "action": "check_wallet_payment",
  "retry": false
}
```

Falha na verificação criptográfica. O preimage não comprova o pagamento desta fatura.

***

## Tratamento de erros em agentes

```typescript theme={null}
import { l402 } from "l402-kit/agent";

try {
  const res = await client.fetch("https://api.example.com/data");
} catch (err: any) {
  if (err.error === "BUDGET_EXCEEDED") {
    // parar e reportar ao usuário
  } else if (err.retry === true) {
    // seguro tentar novamente após breve pausa
    await sleep(1000);
    const res = await client.fetch("https://api.example.com/data");
  }
}
```
