This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.js
77 lines (72 loc) · 2 KB
/
error.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
73
74
75
76
77
const { MulterError } = require("multer")
const { ValidationError, NotFoundError } = require("objection")
const {
DBError,
UniqueViolationError,
NotNullViolationError,
ForeignKeyViolationError,
CheckViolationError,
DataError
} = require("objection-db-errors")
const debug = require("debug")("bazaar:error")
const log = require("../lib/logger")
module.exports = (err, req, res, next) => {
// errors can happen after the response is sent when sending email
if (res.headersSent) {
log.error(err)
return
}
if ([MulterError, ValidationError, DataError].includes(err.constructor)) {
err.statusCode = 400
} else if (err instanceof NotNullViolationError) {
err.statusCode = 400
err.data = {
column: err.column,
table: err.table
}
} else if (err instanceof CheckViolationError) {
err.statusCode = 400
err.data = {
table: err.table,
constraint: err.constraint
}
} else if (err instanceof NotFoundError) {
err.statusCode = 404
} else if (err instanceof UniqueViolationError) {
err.statusCode = 409
err.data = {
columns: err.columns,
table: err.table,
constraint: err.constraint
}
} else if (err instanceof ForeignKeyViolationError) {
err.statusCode = 409
err.data = {
table: err.table,
constraint: err.constraint
}
} else if (err instanceof DBError) {
err.statusCode = 500
err.name = "UnknownDatabaseError"
} else if (err.type && err.type.startsWith("Stripe")) {
// https://github.com/stripe/stripe-node/blob/master/lib/Error.js#L30
this.name = this.type
this.data = this.detail
} else if (!err.statusCode) {
err.statusCode = 500
err.name = "UnknownError"
}
if (
err.statusCode == 500 ||
process.env.NODE_ENV == "development" ||
err.name.startsWith("AlgoliaSearch")
)
log.error(err)
debug("BODY %o", req.body)
debug("USER %o", req.user)
res.status(err.statusCode).send({
message: err.message,
name: err.name,
data: err.data || {}
})
}