Suggestion: Throw Errors insead of Logging
Enderchief opened this issue · 1 comments
Enderchief commented
Whenever an argument is missing or something goes wrong the code currently just does console.log
. While this is great for telling someone what went wrong, it can cause a problem if someone's program was to do an action based on this. I suggest throwing a custom error instead. This way, one can import the error and, if they want, use try and catch specifically for that or do something in there program differently if it does not succeed.
This is an example of what I mean. (with some removed code)
// discord.js-ghost-ping
class ParameterError extends Error {
constructor(...params) {
super(...params)
this.name = "ParameterError"
}
}
const ErrorMessages = {
expectedParameterError: new ParameterError (`\ndiscordjs-ghost-ping: expected parameter (EventType) in dectector()\n\ndiscord.js#client.on(\"messageDelete\", message => {\n ^\n |\n(EventType) is \'messageDelete\'\n`),
...
}
... {
if (eventType === undefined) throw ErrorMessages.expectedParameterError
}
// mybot.js
...
client.on('messageDelete', message => {
// lets say something goes wrong here (misspelled on purpose)
GhostPing.detector('mesageDelete', message)
// One would get this error and know that something in the code is wrong
// ParameterError: `\ndiscordjs-ghost-ping: unexpected parameter (EventType) in dectector()\ ...`
ThatsLiamS commented
Ah thanks, this is a much better way to do that. I'll be implemented in the next update!