-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (72 loc) · 2.06 KB
/
index.js
File metadata and controls
87 lines (72 loc) · 2.06 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const nativeErrorKeys = ['address', 'code', 'errno', 'name', 'path', 'port', 'stack', 'syscall'] // omitting 'message' since that will be applied automatically
// basic error
class ConjureError extends Error {
constructor(arg, options) {
const viaNativeErr = arg instanceof Error
if (viaNativeErr) {
const err = arg
super(err)
for (let i = 0; i < nativeErrorKeys.length; i++) {
const key = nativeErrorKeys[i]
if (err[key] === undefined) {
continue
}
this[key] = err[key]
}
} else {
const message = arg
super(message)
this.message = typeof message === 'string' ? message : message.toString()
}
this.publicMessage = this.publicMessage || this.defaultPublicMessage
this.message = this.message || this.publicMessage
if (options && options.public) {
this.publicMessage = this.message
}
}
get defaultPublicMessage() {
return 'An error occurred'
}
get httpStatusCode() {
return 500
}
}
module.exports.ConjureError = ConjureError
// if something was not found
class NotFoundError extends ConjureError {
get httpStatusCode() {
return 404
}
get defaultPublicMessage() {
return 'A needed resource was not found'
}
}
module.exports.NotFoundError = NotFoundError
// permissions are not valid
class PermissionsError extends ConjureError {
get httpStatusCode() {
return 403
}
get defaultPublicMessage() {
return 'Invalid permissions'
}
}
module.exports.PermissionsError = PermissionsError
// an error occurred where we don't think it should ever have
class UnexpectedError extends ConjureError {
get defaultPublicMessage() {
return 'An unexpected error occurred'
}
}
module.exports.UnexpectedError = UnexpectedError
// missing data, wrong keys passed, etc
class ContentError extends ConjureError {
get defaultPublicMessage() {
return 'Content is missing or invalid'
}
}
module.exports.ContentError = ContentError
// if user triggered an error
class UserError extends ConjureError {
}
module.exports.UserError = UserError