-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors.js
49 lines (39 loc) · 1.77 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
'use strict'
function createError (type, Proto) {
const Err = function (message, cause) {
if (typeof message === 'object' && message !== null) {
// Can be passed just a cause
cause = cause || message
message = message.message || message.name
}
message = message || ''
cause = cause || undefined
// If input is already of type, return as-is to keep its stack trace.
// Avoid instanceof, for when node_modules has multiple copies of level-errors.
if (typeof cause === 'object' && cause.type === type && cause.message === message) {
return cause
}
Object.defineProperty(this, 'type', { value: type, enumerable: false, writable: true, configurable: true })
Object.defineProperty(this, 'name', { value: type, enumerable: false, writable: true, configurable: true })
Object.defineProperty(this, 'cause', { value: cause, enumerable: false, writable: true, configurable: true })
Object.defineProperty(this, 'message', { value: message, enumerable: false, writable: true, configurable: true })
Error.call(this)
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, Err)
}
}
Err.prototype = new Proto()
return Err
}
const LevelUPError = createError('LevelUPError', Error)
module.exports = {
LevelUPError: LevelUPError,
InitializationError: createError('InitializationError', LevelUPError),
OpenError: createError('OpenError', LevelUPError),
ReadError: createError('ReadError', LevelUPError),
WriteError: createError('WriteError', LevelUPError),
NotFoundError: createError('NotFoundError', LevelUPError),
EncodingError: createError('EncodingError', LevelUPError)
}
module.exports.NotFoundError.prototype.notFound = true
module.exports.NotFoundError.prototype.status = 404