mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 11:33:30 +01:00
sorry gus i borked
This commit is contained in:
39
src/client/rest/DiscordAPIError.js
Normal file
39
src/client/rest/DiscordAPIError.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Represents an error from the Discord API
|
||||||
|
*/
|
||||||
|
class DiscordAPIError extends Error {
|
||||||
|
constructor(error) {
|
||||||
|
super();
|
||||||
|
const flattened = error.errors ? `\n${this.constructor.flattenErrors(error.errors).join('\n')}` : '';
|
||||||
|
this.name = 'DiscordAPIError';
|
||||||
|
this.message = `${error.message}${flattened}`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP error code returned by Discord
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
this.code = error.code;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flattens an errors object returned from the API into an array
|
||||||
|
* @param {Object} obj Discord errors object
|
||||||
|
* @param {string} [key] idklol
|
||||||
|
* @returns {string[]}
|
||||||
|
*/
|
||||||
|
static flattenErrors(obj, key = '') {
|
||||||
|
let messages = [];
|
||||||
|
for (const k of Object.keys(obj)) {
|
||||||
|
const newKey = key ? isNaN(k) ? `${key}.${k}` : `${key}[${k}]` : k;
|
||||||
|
if (obj[k]._errors) {
|
||||||
|
messages.push(`${newKey}: ${obj[k]._errors.map(e => e.message).join(' ')}`);
|
||||||
|
} else {
|
||||||
|
messages = messages.concat(this.flattenErrors(obj[k], newKey));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = DiscordAPIError;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
const RequestHandler = require('./RequestHandler');
|
const RequestHandler = require('./RequestHandler');
|
||||||
|
const DiscordAPIError = require('../DiscordAPIError');
|
||||||
|
|
||||||
class BurstRequestHandler extends RequestHandler {
|
class BurstRequestHandler extends RequestHandler {
|
||||||
constructor(restManager, endpoint) {
|
constructor(restManager, endpoint) {
|
||||||
@@ -40,7 +41,7 @@ class BurstRequestHandler extends RequestHandler {
|
|||||||
this.resetTimeout = null;
|
this.resetTimeout = null;
|
||||||
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
|
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
|
||||||
} else {
|
} else {
|
||||||
item.reject(err);
|
item.reject(err.status === 400 ? new DiscordAPIError(res.body) : err);
|
||||||
this.handle();
|
this.handle();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const RequestHandler = require('./RequestHandler');
|
const RequestHandler = require('./RequestHandler');
|
||||||
|
const DiscordAPIError = require('../DiscordAPIError');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles API Requests sequentially, i.e. we wait until the current request is finished before moving onto
|
* Handles API Requests sequentially, i.e. we wait until the current request is finished before moving onto
|
||||||
@@ -48,42 +49,39 @@ class SequentialRequestHandler extends RequestHandler {
|
|||||||
execute(item) {
|
execute(item) {
|
||||||
this.busy = true;
|
this.busy = true;
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
item.request
|
item.request.gen().end((err, res) => {
|
||||||
.gen()
|
if (res && res.headers) {
|
||||||
.on('error', e => item.reject(e))
|
this.requestLimit = Number(res.headers['x-ratelimit-limit']);
|
||||||
.end((err, res) => {
|
this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
|
||||||
if (res && res.headers) {
|
this.requestRemaining = Number(res.headers['x-ratelimit-remaining']);
|
||||||
this.requestLimit = Number(res.headers['x-ratelimit-limit']);
|
this.timeDifference = Date.now() - new Date(res.headers.date).getTime();
|
||||||
this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
|
}
|
||||||
this.requestRemaining = Number(res.headers['x-ratelimit-remaining']);
|
if (err) {
|
||||||
this.timeDifference = Date.now() - new Date(res.headers.date).getTime();
|
if (err.status === 429) {
|
||||||
}
|
this.queue.unshift(item);
|
||||||
if (err) {
|
this.restManager.client.setTimeout(() => {
|
||||||
if (err.status === 429) {
|
this.globalLimit = false;
|
||||||
this.queue.unshift(item);
|
resolve();
|
||||||
this.restManager.client.setTimeout(() => {
|
}, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
|
||||||
this.globalLimit = false;
|
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
|
||||||
resolve();
|
|
||||||
}, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
|
|
||||||
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
|
|
||||||
} else {
|
|
||||||
item.reject(err);
|
|
||||||
resolve(err);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this.globalLimit = false;
|
item.reject(err.status === 400 ? new DiscordAPIError(res.body) : err);
|
||||||
const data = res && res.body ? res.body : {};
|
resolve(err);
|
||||||
item.resolve(data);
|
}
|
||||||
if (this.requestRemaining === 0) {
|
} else {
|
||||||
this.restManager.client.setTimeout(
|
this.globalLimit = false;
|
||||||
|
const data = res && res.body ? res.body : {};
|
||||||
|
item.resolve(data);
|
||||||
|
if (this.requestRemaining === 0) {
|
||||||
|
this.restManager.client.setTimeout(
|
||||||
() => resolve(data),
|
() => resolve(data),
|
||||||
this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset
|
this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
resolve(data);
|
resolve(data);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ exports.DefaultOptions = {
|
|||||||
version: 6,
|
version: 6,
|
||||||
},
|
},
|
||||||
http: {
|
http: {
|
||||||
version: 6,
|
version: 7,
|
||||||
host: 'https://discordapp.com',
|
host: 'https://discordapp.com',
|
||||||
cdn: 'https://cdn.discordapp.com',
|
cdn: 'https://cdn.discordapp.com',
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user