Skip to content

Commit 3d3a3ac

Browse files
committed
fix: improve error handling for HTTP 4xx errors
1 parent 9f2ce3f commit 3d3a3ac

File tree

3 files changed

+620
-47
lines changed

3 files changed

+620
-47
lines changed

docs/tools/fetch.md

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Fetch Tool
2+
3+
The `fetch` tool allows MyCoder to make HTTP requests to external APIs. It uses the native Node.js fetch API and includes robust error handling capabilities.
4+
5+
## Basic Usage
6+
7+
```javascript
8+
const response = await fetch({
9+
method: 'GET',
10+
url: 'https://api.example.com/data',
11+
headers: {
12+
Authorization: 'Bearer token123',
13+
},
14+
});
15+
16+
console.log(response.status); // HTTP status code
17+
console.log(response.body); // Response body
18+
```
19+
20+
## Parameters
21+
22+
| Parameter | Type | Required | Description |
23+
| ---------- | ------- | -------- | ------------------------------------------------------------------------- |
24+
| method | string | Yes | HTTP method to use (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS) |
25+
| url | string | Yes | URL to make the request to |
26+
| params | object | No | Query parameters to append to the URL |
27+
| body | object | No | Request body (for POST, PUT, PATCH requests) |
28+
| headers | object | No | Request headers |
29+
| maxRetries | number | No | Maximum number of retries for 4xx errors (default: 3, max: 5) |
30+
| retryDelay | number | No | Initial delay in ms before retrying (default: 1000, min: 100, max: 30000) |
31+
| slowMode | boolean | No | Enable slow mode to avoid rate limits (default: false) |
32+
33+
## Error Handling
34+
35+
The fetch tool includes sophisticated error handling for different types of HTTP errors:
36+
37+
### 400 Bad Request Errors
38+
39+
When a 400 Bad Request error occurs, the fetch tool will automatically retry the request with exponential backoff. This helps handle temporary issues or malformed requests.
40+
41+
```javascript
42+
// Fetch with custom retry settings for Bad Request errors
43+
const response = await fetch({
44+
method: 'GET',
45+
url: 'https://api.example.com/data',
46+
maxRetries: 2, // Retry up to 2 times (3 requests total)
47+
retryDelay: 500, // Start with a 500ms delay, then increase exponentially
48+
});
49+
```
50+
51+
### 429 Rate Limit Errors
52+
53+
For 429 Rate Limit Exceeded errors, the fetch tool will:
54+
55+
1. Automatically retry with exponential backoff
56+
2. Respect the `Retry-After` header if provided by the server
57+
3. Switch to "slow mode" to prevent further rate limit errors
58+
59+
```javascript
60+
// Fetch with rate limit handling
61+
const response = await fetch({
62+
method: 'GET',
63+
url: 'https://api.example.com/data',
64+
maxRetries: 5, // Retry up to 5 times for rate limit errors
65+
retryDelay: 1000, // Start with a 1 second delay
66+
});
67+
68+
// Check if slow mode was enabled due to rate limiting
69+
if (response.slowModeEnabled) {
70+
console.log('Slow mode was enabled to handle rate limits');
71+
}
72+
```
73+
74+
### Preemptive Slow Mode
75+
76+
You can enable slow mode preemptively to avoid hitting rate limits in the first place:
77+
78+
```javascript
79+
// Start with slow mode enabled
80+
const response = await fetch({
81+
method: 'GET',
82+
url: 'https://api.example.com/data',
83+
slowMode: true, // Enable slow mode from the first request
84+
});
85+
```
86+
87+
### Network Errors
88+
89+
The fetch tool also handles network errors (such as connection issues) with the same retry mechanism.
90+
91+
## Response Object
92+
93+
The fetch tool returns an object with the following properties:
94+
95+
| Property | Type | Description |
96+
| --------------- | ---------------- | ------------------------------------------------------------------ |
97+
| status | number | HTTP status code |
98+
| statusText | string | HTTP status text |
99+
| headers | object | Response headers |
100+
| body | string or object | Response body (parsed as JSON if content-type is application/json) |
101+
| retries | number | Number of retries performed (if any) |
102+
| slowModeEnabled | boolean | Whether slow mode was enabled |

0 commit comments

Comments
 (0)