Error Handling

Understanding API errors and how to handle them.

Error Response Format

error-response.json
1{
2 "success": false,
3 "error": {
4 "code": "INVALID_API_KEY",
5 "message": "The provided API key is invalid or expired",
6 "status": 401
7 }
8}

Common Error Codes

400Bad RequestInvalid request parameters
401UnauthorizedInvalid or missing API key
403ForbiddenInsufficient permissions
404Not FoundResource does not exist
429Rate LimitedToo many requests
500Server ErrorInternal server error

Error Handling Example

error-handling.js
1try {
2 const response = await fetch("https://api.watheeq.app/v1/documents", {
3 method: "POST",
4 headers: { "Authorization": "Bearer " + apiKey },
5 body: JSON.stringify(documentData)
6 });
7
8 if (!response.ok) {
9 const error = await response.json();
10
11 switch (error.error.code) {
12 case "INVALID_API_KEY":
13 // Handle authentication error
14 break;
15 case "RATE_LIMITED":
16 // Wait and retry
17 await sleep(error.error.retryAfter);
18 break;
19 default:
20 console.error(error.error.message);
21 }
22 }
23} catch (err) {
24 console.error("Network error:", err);
25}