Skip to content
This repository was archived by the owner on Dec 28, 2022. It is now read-only.

Commit 23642ca

Browse files
authored
Adjust error message format and improve stack traces (#96)
* Adjust error message format and improve stack traces * Feedback from @mafintosh * Error constructors are called with different `this`
1 parent d83130c commit 23642ca

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

lib/errors.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
11
module.exports = class HypercoreError extends Error {
2-
constructor (msg, code) {
3-
super(msg)
2+
constructor (msg, code, fn = HypercoreError) {
3+
super(`${code}: ${msg}`)
44
this.code = code
5+
6+
if (Error.captureStackTrace) {
7+
Error.captureStackTrace(this, fn)
8+
}
9+
}
10+
11+
get name () {
12+
return 'HypercoreError'
513
}
614

715
static BAD_ARGUMENT (msg) {
8-
return new HypercoreError(msg, 'BAD_ARGUMENT')
16+
return new HypercoreError(msg, 'BAD_ARGUMENT', HypercoreError.BAD_ARGUMENT)
917
}
1018

1119
static STORAGE_EMPTY (msg) {
12-
return new HypercoreError(msg, 'STORAGE_EMPTY')
20+
return new HypercoreError(msg, 'STORAGE_EMPTY', HypercoreError.STORAGE_EMPTY)
1321
}
1422

1523
static STORAGE_CONFLICT (msg) {
16-
return new HypercoreError(msg, 'STORAGE_CONFLICT')
24+
return new HypercoreError(msg, 'STORAGE_CONFLICT', HypercoreError.STORAGE_CONFLICT)
1725
}
1826

1927
static INVALID_SIGNATURE (msg) {
20-
return new HypercoreError(msg, 'INVALID_SIGNATURE')
28+
return new HypercoreError(msg, 'INVALID_SIGNATURE', HypercoreError.INVALID_SIGNATURE)
2129
}
2230

2331
static INVALID_CAPABILITY (msg) {
24-
return new HypercoreError(msg, 'INVALID_CAPABILITY')
32+
return new HypercoreError(msg, 'INVALID_CAPABILITY', HypercoreError.INVALID_CAPABILITY)
2533
}
2634

2735
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)
2937
}
3038

3139
static REQUEST_CANCELLED (msg = 'Request was cancelled') {
32-
return new HypercoreError(msg, 'REQUEST_CANCELLED')
40+
return new HypercoreError(msg, 'REQUEST_CANCELLED', HypercoreError.REQUEST_CANCELLED)
3341
}
3442

3543
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)
3745
}
3846

3947
static SESSION_CLOSED (msg = 'Session is closed') {
40-
return new HypercoreError(msg, 'SESSION_CLOSED')
48+
return new HypercoreError(msg, 'SESSION_CLOSED', HypercoreError.SESSION_CLOSED)
4149
}
4250
}

0 commit comments

Comments
 (0)