-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.js
72 lines (51 loc) · 2.26 KB
/
errors.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
71
72
'use strict';
/* ------------------------------------------------------------------------ */
module.exports = subclass (
/* Root class */
Error,
/* Derived class hierarchy */
{ 'BaseError':
{ 'ExchangeError':
{ 'NotSupported': {}
, 'AuthenticationError': {}
, 'InvalidNonce': {}
, 'InsufficientFunds': {}
, 'InvalidOrder':
{ 'OrderNotFound': {}
, 'OrderNotCached': {}
, 'CancelPending': {}
}
, 'NetworkError':
{ 'DDoSProtection': {}
, 'RequestTimeout': {}
, 'ExchangeNotAvailable': {}
}
}
}
}
)
/* ------------------------------------------------------------------------ */
function subclass (BaseClass, classes, namespace = {}) {
for (const [$class, subclasses] of Object.entries (classes)) {
const Class = Object.assign (namespace, {
/* By creating a named property, we trick compiler to assign our class constructor function a name.
Otherwise, all our error constructors would be shown as [Function: Error] in the debugger! And
the super-useful `e.constructor.name` magic wouldn't work — we then would have no chance to
obtain a error type string from an error instance programmatically! */
[$class]: class extends BaseClass {
constructor (message) {
super (message)
/* A workaround to make `instanceof` work on custom Error classes in transpiled ES5.
See my blog post for the explanation of this hack:
https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801 */
this.constructor = Class
this.__proto__ = Class.prototype
this.message = message
}
}
})[$class]
subclass (Class, subclasses, namespace)
}
return namespace
}
/* ------------------------------------------------------------------------ */