-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherror.js
More file actions
104 lines (78 loc) · 2.88 KB
/
error.js
File metadata and controls
104 lines (78 loc) · 2.88 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const {format} = require('util')
const {Errors} = require('err-object')
const {E, error} = new Errors({
prefix: '[ctrip-apollo] '
})
const EE = (name, type, desc, unit = 'a') => E(
`INVALID_${name.toUpperCase()}`,
`${desc} must be ${unit} ${type}, but got %s`,
TypeError
)
E('NOT_READY', 'never call %s() before ready')
E('FETCH_STATUS_ERROR', 'config service got response status %s')
E('POLLING_STATUS_ERROR', 'polling response status %s')
// Type checking
///////////////////////////////////////////////////////////////////////
EE('options', 'object', 'options', 'an')
// -> 'INVALID_OPTIONS', and vice versa
EE('host', 'string', 'options.host') // -> 'INVALID_HOST'
EE('appId', 'string', 'options.appId')
EE('cluster', 'string', 'options.cluster')
EE('namespace', 'string', 'options.namespace')
EE('ip', 'string', 'options.ip')
EE('dataCenter', 'string', 'options.dataCenter')
EE('fetchTimeout', 'non-negative number', 'options.fetchTimeout')
EE('fetchInterval', 'number', 'options.refreshInterval')
EE('enableUpdateNotification', 'boolean', 'options.enableUpdateNotification')
EE('pollingRetryPolicy', 'function', 'options.pollingRetryPolicy')
EE('enableFetch', 'boolean', 'options.enableFetch')
EE('cachePath', 'string', 'options.cachePath')
EE('CLUSTER_NAME', 'string', 'cluster')
EE('NAMESPACE_NAME', 'string', 'namespace')
E('INVALID_NAMESPACE_TYPE',
'namespace type must be either "PROPERTIES" or "JSON", but got `%s`')
// Wrap other errors
////////////////////////////////////////////////////////////////////////
const EEE = (code, message) => E(code, {
message
}, ({
preset,
args: [err, ...args]
}) => {
err.originalMessage = format(preset.message, ...args)
err.message = `${err.originalMessage}: ${err.message}`
err.reason = err.message
err.code = code
return err
})
E('FETCH_TIMEOUT', 'fetch timed out for it not completed in %s milliseconds')
EEE('FETCH_REQUEST_ERROR', 'fails to get config')
EEE('JSON_PARSE_ERROR', 'fails to parse JSON')
EEE('POLLING_ERROR', 'polling request fails')
EEE('POLLING_JSON_PARSE_ERROR', 'polling result fails to parse')
// Read cache
///////////////////////////////////////////////////////////////////////
EEE('NO_LOCAL_CACHE_FOUND', 'local cache file "%s" not found or not accessible')
E('NO_CACHE_SPECIFIED', 'options.cachePath not specified')
EEE('READ_LOCAL_CACHE_FAILS', 'fails to read local cache file "%s"')
const composeError = (primary, secondary) => {
primary.message = `${
primary.originalMessage || primary.message
}, and ${
secondary.originalMessage || secondary.message}`
if (primary.reason && secondary.reason) {
primary.message += ', reason:'
}
if (primary.reason) {
primary.message += `\n- ${primary.reason}`
}
if (secondary.reason) {
primary.message += `\n- ${secondary.reason}`
}
primary.codes = [primary.code, secondary.code]
return primary
}
module.exports = {
error,
composeError
}