-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
151 lines (143 loc) · 5.66 KB
/
index.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const isProto = Symbol.for('isProto');
const interpolationRegex = /\{([^}]*)\}/g;
const stringFormatValidator = regex => str => regex.test(str);
const isCamelCase = stringFormatValidator(/^[a-z][a-zA-Z0-9]*$/);
const isAlphaNumeric = stringFormatValidator(/^[a-zA-Z0-9]+$/);
var initialized = false;
var log;
function deprecationWarning(msg, context) {
var e = new Error();
log && log.warn && log.warn(msg, {
$meta: {
mtid: 'deprecation',
method: context.method
},
args: context.args,
error: {
type: 'utError.deprecation',
stack: e.stack.split('\n').splice(3).join('\n')
}
});
}
function inherit(Ctor, SuperCtor) {
Ctor.prototype = (SuperCtor === Error) ? new Error() : new SuperCtor(isProto); // hinting that a prototype object but not a regular instance is being constructed
Ctor.superConstructor = SuperCtor;
Ctor.prototype.constructor = Ctor;
}
function interpolate(message, params) {
return message.replace(interpolationRegex, (placeHolder, label) => {
return typeof params[label] === 'undefined' ? `?${label}?` : params[label];
});
};
function UTError(x) {
if (x === isProto) { // knowing that a prototype object but not a regular instance is being constructed
return;
}
Error.call(this);
Error.captureStackTrace && Error.captureStackTrace(this, this.constructor);
var defaultMessage = this.defaultMessage;
if (x instanceof Error) {
this.cause = x;
} else {
Object.assign(this, x);
}
this.message = interpolate(x.message || defaultMessage || 'Unknown Error', x.params || {});
}
inherit(UTError, Error);
function createErrorConstructor(type, SuperCtor, message, level) {
function CustomUTError(x) {
if (x === isProto) { // knowing that a prototype object but not a regular instance is being constructed
return;
} else if (!(this instanceof CustomUTError)) {
return new CustomUTError(x);
} else if (typeof x !== 'object') {
if (x !== undefined && typeof x !== 'string') deprecationWarning('argument must be an object', {type, args: x, method: 'utError.constructor'});
x = {message: x}; // temporary polyfill
}
SuperCtor.call(this, x);
this.type = type;
if (level && !this.level) this.level = level;
}
inherit(CustomUTError, SuperCtor);
CustomUTError.type = type;
CustomUTError.level = level;
CustomUTError.prototype.defaultMessage = message;
return CustomUTError;
}
var errorTypes = {
};
module.exports = {
init: function(bus) {
if (initialized) {
return;
}
initialized = true;
if (bus && bus.logFactory) {
log = bus.logFactory.createLog(bus.logLevel, {name: 'utError', context: 'utError'});
}
},
define: function(id, superType, message, level) {
if (typeof id !== 'string') {
throw new Error(JSON.stringify({
message: 'wrong id type',
expected: 'string',
actual: typeof id
}, null, 4));
}
if (typeof level === 'object') {
deprecationWarning('level must be string', {args: {id}, method: 'utError.define'});
level = level.level;
}
var SuperCtor = UTError;
if (superType) {
if (!isAlphaNumeric(id)) {
deprecationWarning('error identifier must comprise alphanumeric characters only', {args: {id}, method: 'utError.define'});
}
if (typeof message !== 'string') {
deprecationWarning('missing error message', {args: {id}, method: 'utError.define'});
}
if (typeof superType === 'string' && errorTypes[superType]) {
SuperCtor = errorTypes[superType];
} else if (typeof superType === 'function' && superType.prototype instanceof UTError) {
SuperCtor = superType;
}
} else if (!isCamelCase(id)) {
deprecationWarning('error identifier must be in camelCase format', {args: {id}, method: 'utError.define'});
}
var type = SuperCtor === UTError ? id : SuperCtor.type + '.' + id;
if (errorTypes[type]) {
deprecationWarning(`Error ${id} is already defined! Type: ${type}`, {args: {id: type}, method: 'utError.define'});
// throw error;
}
errorTypes[type] = createErrorConstructor(type, SuperCtor, message, level);
return errorTypes[type];
},
fetch: function(RootError) {
if (!RootError) {
RootError = UTError;
} else {
if (typeof RootError === 'string') {
RootError = errorTypes[RootError];
}
if (!RootError || !(RootError.prototype instanceof UTError)) {
return {}; // maybe throw
}
}
let errors = {};
errors[RootError.type] = RootError;
Object.keys(errorTypes).forEach(type => {
if (errorTypes[type].prototype instanceof RootError) {
errors[type] = errorTypes[type];
}
});
return errors;
},
get: function(type) {
if (!type) { // TODO: remove check. use fetch for fetching multiple error definitions
return errorTypes;
}
return errorTypes[type];
// TODO: should we return unknownType error or leave it undefined?
// return errorTypes[type] || errorTypes.unknownType.bind(null, {params: {type}});
}
};