Docs/Advanced/Error Handling

Error Handling

Handle API errors gracefully with typed error responses.

The SDK throws ForgeAPIError on non-2xx responses. Rate limits (429) are automatically retried up to 3 times.

Catching Errors

1import { ForgeClient, ForgeAPIError } from "discordforge-sdk";
2 
3try {
4 await forge.postStats({ serverCount: 1500 });
5} catch (error) {
6 if (error instanceof ForgeAPIError) {
7 console.error(`API error ${error.status}: ${error.message}`);
8 console.error("Response body:", error.body);
9 
10 if (error.retryAfter) {
11 console.log(`Rate limited – retry after ${error.retryAfter}s`);
12 }
13 }
14}

Error Properties

  • error.status – HTTP status code (401, 404, 422, 429, etc.)
  • error.body – Parsed response body from the API
  • error.retryAfter – Seconds until you can retry (from retry-after header)
  • error.resetAfter – Rate limit reset time (from x-ratelimit-reset header)

HTTP Status Codes

  • 200 – Success
  • 204 – Success (no content)
  • 401 – Missing or invalid API key
  • 404 – Bot or resource not found
  • 422 – Validation error (bad request body)
  • 429 – Rate limit exceeded (auto-retried)
  • 500 – Server error