forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DJSError.js
48 lines (43 loc) · 1.31 KB
/
DJSError.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
// Heavily inspired by node's `internal/errors` module
const ErrorCodes = require('./ErrorCodes');
const Messages = require('./Messages');
/**
* Extend an error of some sort into a DiscordjsError.
* @param {Error} Base Base error to extend
* @returns {DiscordjsError}
* @ignore
*/
function makeDiscordjsError(Base) {
return class DiscordjsError extends Base {
constructor(code, ...args) {
super(message(code, args));
this.code = code;
Error.captureStackTrace?.(this, DiscordjsError);
}
get name() {
return `${super.name} [${this.code}]`;
}
};
}
/**
* Format the message for an error.
* @param {string} code The error code
* @param {Array<*>} args Arguments to pass for util format or as function args
* @returns {string} Formatted string
* @ignore
*/
function message(code, args) {
if (!(code in ErrorCodes)) throw new Error('Error code must be a valid DiscordjsErrorCodes');
const msg = Messages[code];
if (!msg) throw new Error(`No message associated with error code: ${code}.`);
if (typeof msg === 'function') return msg(...args);
if (!args?.length) return msg;
args.unshift(msg);
return String(...args);
}
module.exports = {
Error: makeDiscordjsError(Error),
TypeError: makeDiscordjsError(TypeError),
RangeError: makeDiscordjsError(RangeError),
};