Skip to content

Commit d234165

Browse files
authored
fix(Rest): resolved a regression, added retried AbortError (#4852)
1 parent 169d4c3 commit d234165

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/rest/RequestHandler.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,13 @@ class RequestHandler {
8888
try {
8989
res = await request.make();
9090
} catch (error) {
91-
// NodeFetch error expected for all "operational" errors, such as 500 status code
92-
throw new HTTPError(error.message, error.constructor.name, error.status, request.method, request.path);
91+
// Retry the specified number of times for request abortions
92+
if (request.retries === this.manager.client.options.retryLimit) {
93+
throw new HTTPError(error.message, error.constructor.name, error.status, request.method, request.path);
94+
}
95+
96+
request.retries++;
97+
return this.execute(request);
9398
}
9499

95100
if (res && res.headers) {
@@ -122,20 +127,34 @@ class RequestHandler {
122127
}
123128
}
124129

130+
// Handle 2xx and 3xx responses
125131
if (res.ok) {
126132
// Nothing wrong with the request, proceed with the next one
127133
return parseResponse(res);
128134
}
129135

130-
// Handle ratelimited requests
131-
if (res.status === 429) {
132-
// A ratelimit was hit - this should never happen
133-
this.manager.client.emit('debug', `429 hit on route ${request.route}`);
134-
await Util.delayFor(this.retryAfter);
135-
return this.execute(request);
136+
// Handle 4xx responses
137+
if (res.status >= 400 && res.status < 500) {
138+
// Handle ratelimited requests
139+
if (res.status === 429) {
140+
// A ratelimit was hit - this should never happen
141+
this.manager.client.emit('debug', `429 hit on route ${request.route}`);
142+
await Util.delayFor(this.retryAfter);
143+
return this.execute(request);
144+
}
145+
146+
// Handle possible malformed requests
147+
let data;
148+
try {
149+
data = await parseResponse(res);
150+
} catch (err) {
151+
throw new HTTPError(err.message, err.constructor.name, err.status, request.method, request.path);
152+
}
153+
154+
throw new DiscordAPIError(request.path, data, request.method, res.status);
136155
}
137156

138-
// Handle server errors
157+
// Handle 5xx responses
139158
if (res.status >= 500 && res.status < 600) {
140159
// Retry the specified number of times for possible serverside issues
141160
if (request.retries === this.manager.client.options.retryLimit) {
@@ -146,16 +165,8 @@ class RequestHandler {
146165
return this.execute(request);
147166
}
148167

149-
// Handle possible malformed requests
150-
try {
151-
const data = await parseResponse(res);
152-
if (res.status >= 400 && res.status < 500) {
153-
throw new DiscordAPIError(request.path, data, request.method, res.status);
154-
}
155-
return null;
156-
} catch (err) {
157-
throw new HTTPError(err.message, err.constructor.name, err.status, request.method, request.path);
158-
}
168+
// Fallback in the rare case a status code outside the range 200..=599 is returned
169+
return null;
159170
}
160171
}
161172

0 commit comments

Comments
 (0)