Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Rest): show the data that is sent to Discord when an errors occurs #5701

Merged
merged 3 commits into from Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/rest/DiscordAPIError.js
Expand Up @@ -5,7 +5,7 @@
* @extends Error
*/
class DiscordAPIError extends Error {
constructor(path, error, method, status) {
constructor(error, status, request) {
super();
const flattened = this.constructor.flattenErrors(error.errors || error).join('\n');
this.name = 'DiscordAPIError';
Expand All @@ -15,13 +15,13 @@ class DiscordAPIError extends Error {
* The HTTP method used for the request
* @type {string}
*/
this.method = method;
this.method = request.method;

/**
* The path of the request relative to the HTTP endpoint
* @type {string}
*/
this.path = path;
this.path = request.path;

/**
* HTTP error code returned by Discord
Expand All @@ -34,6 +34,15 @@ class DiscordAPIError extends Error {
* @type {number}
*/
this.httpStatus = status;

/**
* The data associated with the request that caused this error
* @type {HTTPErrorData}
*/
this.requestData = {
json: request.options.data,
files: request.options.files ?? [],
};
}

/**
Expand Down
29 changes: 26 additions & 3 deletions src/rest/HTTPError.js
Expand Up @@ -5,7 +5,7 @@
* @extends Error
*/
class HTTPError extends Error {
constructor(message, name, code, method, path) {
constructor(message, name, code, request) {
super(message);

/**
Expand All @@ -24,13 +24,36 @@ class HTTPError extends Error {
* The HTTP method used for the request
* @type {string}
*/
this.method = method;
this.method = request.method;

/**
* The path of the request relative to the HTTP endpoint
* @type {string}
*/
this.path = path;
this.path = request.path;

/**
* The HTTP data that was sent to Discord
* @typedef {Object} HTTPErrorData
* @property {*} json The JSON data that was sent
* @property {HTTPAttachmentData[]} files The files that were sent with this request, if any
*/

/**
* The attachment data that is sent to Discord
* @typedef {Object} HTTPAttachmentData
* @property {string} name The file name
* @property {Buffer} file The file buffer
*/

/**
* The data associated with the request that caused this error
* @type {HTTPErrorData}
*/
this.requestData = {
json: request.options.data,
files: request.options.files ?? [],
};
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/rest/RequestHandler.js
Expand Up @@ -143,7 +143,7 @@ class RequestHandler {
} catch (error) {
// Retry the specified number of times for request abortions
if (request.retries === this.manager.client.options.retryLimit) {
throw new HTTPError(error.message, error.constructor.name, error.status, request.method, request.path);
throw new HTTPError(error.message, error.constructor.name, error.status, request);
}

request.retries++;
Expand Down Expand Up @@ -237,17 +237,17 @@ class RequestHandler {
try {
data = await parseResponse(res);
} catch (err) {
throw new HTTPError(err.message, err.constructor.name, err.status, request.method, request.path);
throw new HTTPError(err.message, err.constructor.name, err.status, request);
}

throw new DiscordAPIError(request.path, data, request.method, res.status);
throw new DiscordAPIError(data, res.status, request);
}

// Handle 5xx responses
if (res.status >= 500 && res.status < 600) {
// Retry the specified number of times for possible serverside issues
if (request.retries === this.manager.client.options.retryLimit) {
throw new HTTPError(res.statusText, res.constructor.name, res.status, request.method, request.path);
throw new HTTPError(res.statusText, res.constructor.name, res.status, request);
}

request.retries++;
Expand Down
7 changes: 4 additions & 3 deletions src/structures/APIMessage.js
Expand Up @@ -125,6 +125,7 @@ class APIMessage {
*/
resolveData() {
if (this.data) return this;
const isWebhook = this.isWebhook;
vladfrangu marked this conversation as resolved.
Show resolved Hide resolved

const content = this.makeContent();
const tts = Boolean(this.options.tts);
Expand All @@ -139,7 +140,7 @@ class APIMessage {
}

const embedLikes = [];
if (this.isInteraction || this.isWebhook) {
if (this.isInteraction || isWebhook) {
if (this.options.embeds) {
embedLikes.push(...this.options.embeds);
}
Expand All @@ -150,7 +151,7 @@ class APIMessage {

let username;
let avatarURL;
if (this.isWebhook) {
if (isWebhook) {
username = this.options.username || this.target.name;
if (this.options.avatarURL) avatarURL = this.options.avatarURL;
}
Expand Down Expand Up @@ -192,7 +193,7 @@ class APIMessage {
tts,
nonce,
embed: this.options.embed === null ? null : embeds[0],
embeds,
embeds: isWebhook ? embeds : undefined,
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
username,
avatar_url: avatarURL,
allowed_mentions:
Expand Down