|
1 | 1 | module.exports = class HypercoreError extends Error { |
2 | | - constructor (msg, code) { |
3 | | - super(msg) |
| 2 | + constructor (msg, code, fn = HypercoreError) { |
| 3 | + super(`${code}: ${msg}`) |
4 | 4 | this.code = code |
| 5 | + |
| 6 | + if (Error.captureStackTrace) { |
| 7 | + Error.captureStackTrace(this, fn) |
| 8 | + } |
| 9 | + } |
| 10 | + |
| 11 | + get name () { |
| 12 | + return 'HypercoreError' |
5 | 13 | } |
6 | 14 |
|
7 | 15 | static BAD_ARGUMENT (msg) { |
8 | | - return new HypercoreError(msg, 'BAD_ARGUMENT') |
| 16 | + return new HypercoreError(msg, 'BAD_ARGUMENT', HypercoreError.BAD_ARGUMENT) |
9 | 17 | } |
10 | 18 |
|
11 | 19 | static STORAGE_EMPTY (msg) { |
12 | | - return new HypercoreError(msg, 'STORAGE_EMPTY') |
| 20 | + return new HypercoreError(msg, 'STORAGE_EMPTY', HypercoreError.STORAGE_EMPTY) |
13 | 21 | } |
14 | 22 |
|
15 | 23 | static STORAGE_CONFLICT (msg) { |
16 | | - return new HypercoreError(msg, 'STORAGE_CONFLICT') |
| 24 | + return new HypercoreError(msg, 'STORAGE_CONFLICT', HypercoreError.STORAGE_CONFLICT) |
17 | 25 | } |
18 | 26 |
|
19 | 27 | static INVALID_SIGNATURE (msg) { |
20 | | - return new HypercoreError(msg, 'INVALID_SIGNATURE') |
| 28 | + return new HypercoreError(msg, 'INVALID_SIGNATURE', HypercoreError.INVALID_SIGNATURE) |
21 | 29 | } |
22 | 30 |
|
23 | 31 | static INVALID_CAPABILITY (msg) { |
24 | | - return new HypercoreError(msg, 'INVALID_CAPABILITY') |
| 32 | + return new HypercoreError(msg, 'INVALID_CAPABILITY', HypercoreError.INVALID_CAPABILITY) |
25 | 33 | } |
26 | 34 |
|
27 | 35 | static SNAPSHOT_NOT_AVAILABLE (msg = 'Snapshot is not available') { |
28 | | - return new HypercoreError(msg, 'SNAPSHOT_NOT_AVAILABLE') |
| 36 | + return new HypercoreError(msg, 'SNAPSHOT_NOT_AVAILABLE', HypercoreError.SNAPSHOT_NOT_AVAILABLE) |
29 | 37 | } |
30 | 38 |
|
31 | 39 | static REQUEST_CANCELLED (msg = 'Request was cancelled') { |
32 | | - return new HypercoreError(msg, 'REQUEST_CANCELLED') |
| 40 | + return new HypercoreError(msg, 'REQUEST_CANCELLED', HypercoreError.REQUEST_CANCELLED) |
33 | 41 | } |
34 | 42 |
|
35 | 43 | static SESSION_NOT_WRITABLE (msg = 'Session is not writable') { |
36 | | - return new HypercoreError(msg, 'SESSION_NOT_WRITABLE') |
| 44 | + return new HypercoreError(msg, 'SESSION_NOT_WRITABLE', HypercoreError.SESSION_NOT_WRITABLE) |
37 | 45 | } |
38 | 46 |
|
39 | 47 | static SESSION_CLOSED (msg = 'Session is closed') { |
40 | | - return new HypercoreError(msg, 'SESSION_CLOSED') |
| 48 | + return new HypercoreError(msg, 'SESSION_CLOSED', HypercoreError.SESSION_CLOSED) |
41 | 49 | } |
42 | 50 | } |
0 commit comments