Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Created March 27, 2019 11:02
Show Gist options
  • Save DarkSeraphim/55b9c5e417e6d9e76631a27655e3f7d8 to your computer and use it in GitHub Desktop.
Save DarkSeraphim/55b9c5e417e6d9e76631a27655e3f7d8 to your computer and use it in GitHub Desktop.
Ignoring Discord.js errors that are expected and ignoreable
const { Constants: { APIErrors }} = require('discord.js');
const errorCodes = new Set(Object.values(APIErrors));
/**
* Usage:
* const {APIErrors: {CANNOT_MESSAGE_USER}, ignore} = require('./ignore-errors.js');
* try {
* let message = await user.send('Hello world').catch(ignore(CANNOT_MESSAGE_USER));
* if (message === null) {
* // User has DMs closed
* } else {
* // Message was sent!
* }
* } catch (e) {
* // Real error happened! O no!
* }
*/
module.exports.APIErrors = APIErrors;
module.exports.ignore = (...errors) => {
if (errors.find(error => !errorCodes.has(error))) {
throw new Error('Cannot ignore unknown error. Please check your error')
}
let ignore = new Set(errors);
return error => {
if (!errors.has(error)) {
throw error;
}
return null;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment