Errors
RailwailError
All API errors throw a RailwailError with a status code, error type, and human-readable message.
Import
TypeScript
import { RailwailError } from "railwail";Properties
| Property | Type | Description |
|---|---|---|
| message | string | Human-readable error message |
| status | number | HTTP status code (401, 402, 429, 500, etc.) |
| type | string | Error category (e.g. "authentication_error", "rate_limit_error") |
| code | string | Specific error code (e.g. "invalid_api_key", "insufficient_credits") |
Error Handling
TypeScript
import railwail, { RailwailError } from "railwail";
const rw = railwail("rw_live_xxxxx");
try {
const reply = await rw.run("gpt-4o", "Hello!");
console.log(reply);
} catch (err) {
if (err instanceof RailwailError) {
console.error(`Status: ${err.status}`); // 401
console.error(`Type: ${err.type}`); // "authentication_error"
console.error(`Code: ${err.code}`); // "invalid_api_key"
console.error(`Message: ${err.message}`); // "Invalid API key provided"
// Handle specific errors
switch (err.status) {
case 401:
console.error("Check your API key");
break;
case 402:
console.error("Add credits at railwail.com/billing");
break;
case 429:
console.error("Rate limited — retry after cooldown");
break;
}
} else {
// Network error, timeout, etc.
console.error("Unexpected error:", err);
}
}Common Error Codes
| Status | Code | Description |
|---|---|---|
400 | bad_request | Malformed request body or missing required fields |
401 | invalid_api_key | Invalid or missing API key |
402 | insufficient_credits | Account does not have enough credits |
429 | rate_limit_exceeded | Too many requests — retry after cooldown |
500 | internal_error | Server error — try again later |
See the full Error Codes reference for all error types and suggested handling strategies.