Tratamento de Erros
A API Fortbix retorna erros estruturados em JSON.
Formato Padrão
{
"error": "invalid_request",
"message": "Parâmetro 'amount' é obrigatório",
"code": "INVALID_REQUEST",
"details": {
"field": "amount",
"reason": "required"
}
}
Códigos HTTP
| Status | Significado | Ação |
|---|---|---|
| 400 | Bad Request | Verifique parâmetros |
| 401 | Unauthorized | Verifique API Key |
| 403 | Forbidden | Sem permissão |
| 404 | Not Found | Recurso não existe |
| 409 | Conflict | Operação conflitante |
| 429 | Too Many Requests | Rate limit excedido |
| 500 | Server Error | Tente novamente |
| 503 | Service Unavailable | Manutenção em andamento |
Tratamento Recomendado
async function handleRequest(url, options) {
try {
const response = await fetch(url, options)
const data = await response.json()
if (!response.ok) {
console.error(`[${data.code}] ${data.message}`)
if (response.status === 429) {
// Retry com backoff
await delay(5000)
return handleRequest(url, options)
}
if (response.status >= 500) {
// Erro do servidor, tente novamente
await delay(2000)
return handleRequest(url, options)
}
// Erro do cliente, não tente novamente
throw new Error(data.message)
}
return data
} catch (error) {
console.error('Network error:', error)
throw error
}
}
Erros Comuns
ValidationError
Você enviou dados inválidos:
{
"error": "validation_error",
"details": {
"amount": "must be positive",
"currency": "unsupported currency"
}
}
Solução: Valide seus dados antes de enviar.
AuthenticationError
Suas credenciais são inválidas:
{
"error": "authentication_failed",
"message": "Invalid API Key"
}
Solução: Verifique sua API Key em Settings.
InsufficientFundsError
Garantias insuficientes para a operação:
{
"error": "insufficient_collateral",
"required": 50000,
"available": 35000
}
Solução: Deposite mais garantias ou reduza o valor da operação.