-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (60 loc) · 1.59 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* eslint-disable max-classes-per-file */
"use strict";
// Require Third-party Dependencies
const cleanStack = require("clean-stack");
// CONSTANTS
const kError = Symbol.for("SlimIOError");
class SlimIOError extends Error {
constructor(message) {
super(message);
Object.defineProperty(this, kError, { value: true });
this.stack = cleanStack(this.stack);
}
toJSON() {
return {
name: `${kError.description}::${this.name}`,
message: this.message,
stack: this.stack
};
}
}
class CallbackArgumentType extends SlimIOError {
constructor(message) {
super(message);
this.name = "InvalidCallbackArgument";
}
}
class CallbackNotFound extends SlimIOError {
constructor(callbackName) {
super(`Unable to found callback with name '${callbackName}'`);
this.name = "CallbackNotFound";
}
}
class RequiredArgument extends SlimIOError {
constructor(argName) {
super(`Argument ${argName} is required`);
this.name = "RequiredArgument";
}
}
/**
* @function isSlimIOError
* @description check and tell if a given object is a SlimIO Error or not!
* @param {*} obj
* @returns {boolean}
*/
function isSlimIOError(obj) {
if (typeof obj !== "object" || obj === null) {
return false;
}
if (obj[kError]) {
return true;
}
return "name" in obj && typeof obj.name === "string" && obj.name.startsWith(kError.description);
}
module.exports = {
isSlimIOError,
SlimIOError,
CallbackArgumentType,
CallbackNotFound,
RequiredArgument
};