generated from ecomplus/application-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error-handling.js
59 lines (54 loc) · 1.39 KB
/
error-handling.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
const ignoreError = response => {
// check response status code
// should ignore some error responses
const { status, data } = response
if (status >= 400 && status < 500) {
switch (status) {
case 403:
// ignore resource limits errors
return true
case 404:
if (data && data.error_code !== 20) {
// resource ID not found ?
// ignore
return true
}
break
}
// must debug
return false
}
}
module.exports = err => {
// axios error object
// https://github.com/axios/axios#handling-errors
if (!err.appAuthRemoved && !err.appErrorLog) {
// error not treated by App SDK
if (err.response) {
if (ignoreError(err.response)) {
// ignore client error
return
}
err.responseJSON = JSON.stringify(err.response.data)
}
// debug unexpected response
console.error(err)
} else if (err.appErrorLog && !err.appErrorLogged) {
// cannot log to app hidden data
// debug app log error
const error = err.appErrorLog
const { response, config } = error
// handle error response
if (response) {
if (ignoreError(response)) {
return
}
// debug unexpected response
error.configJSON = {
originalRequest: JSON.stringify(err.config),
logRequest: JSON.stringify(config)
}
console.error(error)
}
}
}