Errors Standardization (#1246)

* errors and stuff

* more errors

* all the errors

* fix build
This commit is contained in:
Gus Caplan
2017-06-25 12:48:05 -05:00
committed by Amish Shah
parent 602fe06f88
commit 63e54982f4
28 changed files with 258 additions and 102 deletions

View File

@@ -1,4 +1,5 @@
exports.Package = require('../../package.json');
const { Error, RangeError } = require('../errors');
/**
* Options for a client.
@@ -78,20 +79,6 @@ exports.WSCodes = {
4011: 'Shard would be on too many guilds if connected',
};
exports.Errors = {
NO_TOKEN: 'Request to use token, but token was unavailable to the client.',
NO_BOT_ACCOUNT: 'Only bot accounts are able to make use of this feature.',
NO_USER_ACCOUNT: 'Only user accounts are able to make use of this feature.',
BAD_WS_MESSAGE: 'A bad message was received from the websocket; either bad compression, or not JSON.',
TOOK_TOO_LONG: 'Something took too long to do.',
NOT_A_PERMISSION: 'Invalid permission string or number.',
INVALID_RATE_LIMIT_METHOD: 'Unknown rate limiting method.',
BAD_LOGIN: 'Incorrect login details were provided.',
INVALID_SHARD: 'Invalid shard settings were provided.',
SHARDING_REQUIRED: 'This session would have handled too many guilds - Sharding is required.',
INVALID_TOKEN: 'An invalid token was provided.',
};
const AllowedImageFormats = [
'webp',
'png',
@@ -108,8 +95,8 @@ const AllowedImageSizes = [
];
function checkImage({ size, format }) {
if (format && !AllowedImageFormats.includes(format)) throw new Error(`Invalid image format: ${format}`);
if (size && !AllowedImageSizes.includes(size)) throw new RangeError(`Invalid size: ${size}`);
if (format && !AllowedImageFormats.includes(format)) throw new Error('IMAGE_FORMAT', format);
if (size && !AllowedImageSizes.includes(size)) throw new RangeError('IMAGE_SIZE', size);
}
exports.Endpoints = {

View File

@@ -1,4 +1,4 @@
const Constants = require('../util/Constants');
const { RangeError } = require('../errors');
/**
* Data structure that makes it easy to interact with a permission bitfield. All {@link GuildMember}s have a set of
@@ -95,7 +95,7 @@ class Permissions {
static resolve(permission) {
if (permission instanceof Array) return permission.map(p => this.resolve(p)).reduce((prev, p) => prev | p, 0);
if (typeof permission === 'string') permission = this.FLAGS[permission];
if (typeof permission !== 'number' || permission < 1) throw new RangeError(Constants.Errors.NOT_A_PERMISSION);
if (typeof permission !== 'number' || permission < 1) throw new RangeError('PERMISSION_INVALID');
return permission;
}
}

View File

@@ -1,6 +1,7 @@
const snekfetch = require('snekfetch');
const Constants = require('./Constants');
const ConstantsHttp = Constants.DefaultOptions.http;
const { RangeError, TypeError } = require('../errors');
/**
* Contains various general-purpose utility methods. These functions are also available on the base `Discord` object.
@@ -19,7 +20,9 @@ class Util {
static splitMessage(text, { maxLength = 1950, char = '\n', prepend = '', append = '' } = {}) {
if (text.length <= maxLength) return text;
const splitText = text.split(char);
if (splitText.length === 1) throw new Error('Message exceeds the max length and contains no split characters.');
if (splitText.length === 1) {
throw new RangeError('SPLIT_MAX_LEN');
}
const messages = [''];
let msg = 0;
for (let i = 0; i < splitText.length; i++) {
@@ -54,7 +57,7 @@ class Util {
*/
static fetchRecommendedShards(token, guildsPerShard = 1000) {
return new Promise((resolve, reject) => {
if (!token) throw new Error('A token must be provided.');
if (!token) throw new Error('TOKEN_MISSING');
snekfetch.get(`${ConstantsHttp.host}/api/v${ConstantsHttp.version}${Constants.Endpoints.botGateway}`)
.set('Authorization', `Bot ${token.replace(/^Bot\s*/i, '')}`)
.end((err, res) => {
@@ -279,9 +282,9 @@ class Util {
}
if (color < 0 || color > 0xFFFFFF) {
throw new RangeError('Color must be within the range 0 - 16777215 (0xFFFFFF).');
throw new RangeError('COLOR_RANGE');
} else if (color && isNaN(color)) {
throw new TypeError('Unable to convert color to a number.');
throw new TypeError('COLOR_CONVERT');
}
return color;