@merkur/plugin-error 0.37.9 → 0.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +20 -59
- package/lib/index.es9.cjs +0 -4
- package/lib/index.es9.mjs +0 -4
- package/lib/index.js +20 -59
- package/lib/index.mjs +20 -59
- package/lib/index.umd.js +1 -1
- package/package.json +5 -6
- package/server/index.js +0 -65
package/lib/index.cjs
CHANGED
|
@@ -18,20 +18,13 @@ var core = require('@merkur/core');
|
|
|
18
18
|
* call stack frames are dropped by the JS engine).
|
|
19
19
|
* This flag is enabled by default.
|
|
20
20
|
*/
|
|
21
|
-
function ExtensibleError(
|
|
22
|
-
message,
|
|
23
|
-
dropInternalStackFrames = true,
|
|
24
|
-
) {
|
|
21
|
+
function ExtensibleError(message, dropInternalStackFrames = true) {
|
|
25
22
|
if (!(this instanceof ExtensibleError)) {
|
|
26
23
|
throw new TypeError('Cannot call a class as a function');
|
|
27
24
|
}
|
|
28
25
|
if (this.constructor === ExtensibleError) {
|
|
29
|
-
throw new TypeError(
|
|
30
|
-
'The ExtensibleError is an abstract class and ' +
|
|
31
|
-
'must be extended before it can be instantiated.',
|
|
32
|
-
);
|
|
26
|
+
throw new TypeError('The ExtensibleError is an abstract class and ' + 'must be extended before it can be instantiated.');
|
|
33
27
|
}
|
|
34
|
-
|
|
35
28
|
Error.call(this, message); // super-constructor call;
|
|
36
29
|
|
|
37
30
|
/**
|
|
@@ -83,7 +76,6 @@ function ExtensibleError(
|
|
|
83
76
|
*/
|
|
84
77
|
this._dropInternalStackFrames = dropInternalStackFrames;
|
|
85
78
|
}
|
|
86
|
-
|
|
87
79
|
ExtensibleError.prototype = Object.create(Error.prototype);
|
|
88
80
|
ExtensibleError.prototype.constructor = ExtensibleError;
|
|
89
81
|
|
|
@@ -100,7 +92,6 @@ Object.defineProperty(ExtensibleError.prototype, 'stack', {
|
|
|
100
92
|
if (this._stack) {
|
|
101
93
|
return this._stack;
|
|
102
94
|
}
|
|
103
|
-
|
|
104
95
|
let stack = this._nativeError.stack;
|
|
105
96
|
if (typeof stack !== 'string') {
|
|
106
97
|
return undefined;
|
|
@@ -110,7 +101,6 @@ Object.defineProperty(ExtensibleError.prototype, 'stack', {
|
|
|
110
101
|
// constructors
|
|
111
102
|
if (this._dropInternalStackFrames) {
|
|
112
103
|
let stackLines = stack.split('\n');
|
|
113
|
-
|
|
114
104
|
let inheritanceDepth = 1;
|
|
115
105
|
let currentPrototype = Object.getPrototypeOf(this);
|
|
116
106
|
while (currentPrototype !== ExtensibleError.prototype) {
|
|
@@ -118,75 +108,59 @@ Object.defineProperty(ExtensibleError.prototype, 'stack', {
|
|
|
118
108
|
inheritanceDepth++;
|
|
119
109
|
}
|
|
120
110
|
stackLines.splice(1, inheritanceDepth);
|
|
121
|
-
|
|
122
111
|
this._stack = stackLines.join('\n');
|
|
123
112
|
} else {
|
|
124
113
|
this._stack = stack;
|
|
125
114
|
}
|
|
126
|
-
|
|
127
115
|
return this._stack;
|
|
128
|
-
}
|
|
116
|
+
}
|
|
129
117
|
});
|
|
130
118
|
|
|
131
119
|
class GenericError extends ExtensibleError {
|
|
132
120
|
constructor(message, params) {
|
|
133
121
|
super(message);
|
|
134
|
-
const {
|
|
135
|
-
|
|
122
|
+
const {
|
|
123
|
+
status = 500,
|
|
124
|
+
...otherParams
|
|
125
|
+
} = params;
|
|
136
126
|
this.name = 'Error';
|
|
137
127
|
this.status = status;
|
|
138
|
-
|
|
139
128
|
this._params = otherParams;
|
|
140
129
|
}
|
|
141
|
-
|
|
142
130
|
get params() {
|
|
143
131
|
return this._params;
|
|
144
132
|
}
|
|
145
133
|
}
|
|
146
134
|
|
|
147
135
|
const DEV = 'development';
|
|
148
|
-
const ENV =
|
|
149
|
-
typeof process !== 'undefined' && process && process.env
|
|
150
|
-
? process.env.NODE_ENV
|
|
151
|
-
: DEV;
|
|
152
|
-
|
|
136
|
+
const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
|
|
153
137
|
const ERROR_EVENTS = {
|
|
154
|
-
ERROR: '@merkur/plugin-error.error'
|
|
138
|
+
ERROR: '@merkur/plugin-error.error'
|
|
155
139
|
};
|
|
156
|
-
|
|
157
140
|
function errorPlugin() {
|
|
158
141
|
return {
|
|
159
142
|
async setup(widget) {
|
|
160
|
-
widget.error = widget.error
|
|
161
|
-
|
|
162
|
-
:
|
|
163
|
-
|
|
164
|
-
message: null,
|
|
165
|
-
};
|
|
166
|
-
|
|
143
|
+
widget.error = widget.error ? widget.error : {
|
|
144
|
+
status: null,
|
|
145
|
+
message: null
|
|
146
|
+
};
|
|
167
147
|
return widget;
|
|
168
148
|
},
|
|
169
149
|
async create(widget) {
|
|
170
150
|
if (ENV === DEV) {
|
|
171
151
|
if (!widget.$in.component) {
|
|
172
|
-
throw new Error(
|
|
173
|
-
'You must install missing plugin: npm i @merkur/plugin-component',
|
|
174
|
-
);
|
|
152
|
+
throw new Error('You must install missing plugin: npm i @merkur/plugin-component');
|
|
175
153
|
}
|
|
176
154
|
if (!widget.$in.eventEmitter) {
|
|
177
|
-
throw new Error(
|
|
178
|
-
'You must install missing plugin: npm i @merkur/plugin-event-emitter',
|
|
179
|
-
);
|
|
155
|
+
throw new Error('You must install missing plugin: npm i @merkur/plugin-event-emitter');
|
|
180
156
|
}
|
|
181
157
|
}
|
|
182
|
-
|
|
183
158
|
core.hookMethod(widget, 'info', infoHook);
|
|
184
159
|
core.hookMethod(widget, 'load', loadHook);
|
|
185
160
|
core.hookMethod(widget, 'mount', mountHook);
|
|
186
161
|
core.hookMethod(widget, 'update', updateHook);
|
|
187
|
-
|
|
188
162
|
return widget;
|
|
189
|
-
}
|
|
163
|
+
}
|
|
190
164
|
};
|
|
191
165
|
}
|
|
192
166
|
|
|
@@ -197,32 +171,24 @@ async function loadHook(widget, originalLoad, ...rest) {
|
|
|
197
171
|
if (widget.error.status) {
|
|
198
172
|
return result;
|
|
199
173
|
}
|
|
200
|
-
|
|
201
174
|
try {
|
|
202
175
|
result = await originalLoad(...rest);
|
|
203
176
|
} catch (error) {
|
|
204
177
|
error.status = error.status || 500;
|
|
205
|
-
|
|
206
178
|
setErrorInfo(widget, error);
|
|
207
|
-
emitError(widget, error);
|
|
208
179
|
}
|
|
209
|
-
|
|
210
180
|
return result;
|
|
211
181
|
}
|
|
212
|
-
|
|
213
182
|
async function infoHook(widget, originalInfo, ...rest) {
|
|
214
183
|
const result = await originalInfo(...rest);
|
|
215
|
-
|
|
216
184
|
return {
|
|
217
185
|
error: widget.error,
|
|
218
|
-
...result
|
|
186
|
+
...result
|
|
219
187
|
};
|
|
220
188
|
}
|
|
221
|
-
|
|
222
189
|
async function mountHook(widget, originalMount, ...rest) {
|
|
223
190
|
return renderContent(widget, originalMount, rest);
|
|
224
191
|
}
|
|
225
|
-
|
|
226
192
|
async function updateHook(widget, originalUpdate, ...rest) {
|
|
227
193
|
return renderContent(widget, originalUpdate, rest);
|
|
228
194
|
}
|
|
@@ -233,19 +199,15 @@ function setErrorInfo(widget, error) {
|
|
|
233
199
|
widget.error.status = error.status;
|
|
234
200
|
widget.error.message = error.message;
|
|
235
201
|
widget.error.url = error.params && error.params.url;
|
|
236
|
-
|
|
237
202
|
if (ENV === DEV) {
|
|
238
203
|
widget.error.stack = error.stack;
|
|
239
204
|
}
|
|
205
|
+
widget.emit(ERROR_EVENTS.ERROR, {
|
|
206
|
+
error
|
|
207
|
+
});
|
|
240
208
|
}
|
|
241
|
-
|
|
242
|
-
function emitError(widget, error) {
|
|
243
|
-
widget.emit(ERROR_EVENTS.ERROR, { error });
|
|
244
|
-
}
|
|
245
|
-
|
|
246
209
|
async function renderContent(widget, method, properties) {
|
|
247
210
|
let result = null;
|
|
248
|
-
|
|
249
211
|
if (widget.error.status) {
|
|
250
212
|
// error was captured in an earlier lifecycle method
|
|
251
213
|
try {
|
|
@@ -267,7 +229,6 @@ async function renderContent(widget, method, properties) {
|
|
|
267
229
|
// save error info
|
|
268
230
|
err.status = err.status || 500;
|
|
269
231
|
setErrorInfo(widget, err);
|
|
270
|
-
emitError(widget, err);
|
|
271
232
|
|
|
272
233
|
// try rendering again
|
|
273
234
|
return renderContent(widget, method, properties);
|
package/lib/index.es9.cjs
CHANGED
|
@@ -174,7 +174,6 @@ async function loadHook(widget, originalLoad, ...rest) {
|
|
|
174
174
|
} catch (error) {
|
|
175
175
|
error.status = error.status || 500;
|
|
176
176
|
setErrorInfo(widget, error);
|
|
177
|
-
emitError(widget, error);
|
|
178
177
|
}
|
|
179
178
|
return result;
|
|
180
179
|
}
|
|
@@ -201,8 +200,6 @@ function setErrorInfo(widget, error) {
|
|
|
201
200
|
if (ENV === DEV) {
|
|
202
201
|
widget.error.stack = error.stack;
|
|
203
202
|
}
|
|
204
|
-
}
|
|
205
|
-
function emitError(widget, error) {
|
|
206
203
|
widget.emit(ERROR_EVENTS.ERROR, {
|
|
207
204
|
error
|
|
208
205
|
});
|
|
@@ -230,7 +227,6 @@ async function renderContent(widget, method, properties) {
|
|
|
230
227
|
// save error info
|
|
231
228
|
err.status = err.status || 500;
|
|
232
229
|
setErrorInfo(widget, err);
|
|
233
|
-
emitError(widget, err);
|
|
234
230
|
|
|
235
231
|
// try rendering again
|
|
236
232
|
return renderContent(widget, method, properties);
|
package/lib/index.es9.mjs
CHANGED
|
@@ -172,7 +172,6 @@ async function loadHook(widget, originalLoad, ...rest) {
|
|
|
172
172
|
} catch (error) {
|
|
173
173
|
error.status = error.status || 500;
|
|
174
174
|
setErrorInfo(widget, error);
|
|
175
|
-
emitError(widget, error);
|
|
176
175
|
}
|
|
177
176
|
return result;
|
|
178
177
|
}
|
|
@@ -199,8 +198,6 @@ function setErrorInfo(widget, error) {
|
|
|
199
198
|
if (ENV === DEV) {
|
|
200
199
|
widget.error.stack = error.stack;
|
|
201
200
|
}
|
|
202
|
-
}
|
|
203
|
-
function emitError(widget, error) {
|
|
204
201
|
widget.emit(ERROR_EVENTS.ERROR, {
|
|
205
202
|
error
|
|
206
203
|
});
|
|
@@ -228,7 +225,6 @@ async function renderContent(widget, method, properties) {
|
|
|
228
225
|
// save error info
|
|
229
226
|
err.status = err.status || 500;
|
|
230
227
|
setErrorInfo(widget, err);
|
|
231
|
-
emitError(widget, err);
|
|
232
228
|
|
|
233
229
|
// try rendering again
|
|
234
230
|
return renderContent(widget, method, properties);
|
package/lib/index.js
CHANGED
|
@@ -18,20 +18,13 @@ var core = require('@merkur/core');
|
|
|
18
18
|
* call stack frames are dropped by the JS engine).
|
|
19
19
|
* This flag is enabled by default.
|
|
20
20
|
*/
|
|
21
|
-
function ExtensibleError(
|
|
22
|
-
message,
|
|
23
|
-
dropInternalStackFrames = true,
|
|
24
|
-
) {
|
|
21
|
+
function ExtensibleError(message, dropInternalStackFrames = true) {
|
|
25
22
|
if (!(this instanceof ExtensibleError)) {
|
|
26
23
|
throw new TypeError('Cannot call a class as a function');
|
|
27
24
|
}
|
|
28
25
|
if (this.constructor === ExtensibleError) {
|
|
29
|
-
throw new TypeError(
|
|
30
|
-
'The ExtensibleError is an abstract class and ' +
|
|
31
|
-
'must be extended before it can be instantiated.',
|
|
32
|
-
);
|
|
26
|
+
throw new TypeError('The ExtensibleError is an abstract class and ' + 'must be extended before it can be instantiated.');
|
|
33
27
|
}
|
|
34
|
-
|
|
35
28
|
Error.call(this, message); // super-constructor call;
|
|
36
29
|
|
|
37
30
|
/**
|
|
@@ -83,7 +76,6 @@ function ExtensibleError(
|
|
|
83
76
|
*/
|
|
84
77
|
this._dropInternalStackFrames = dropInternalStackFrames;
|
|
85
78
|
}
|
|
86
|
-
|
|
87
79
|
ExtensibleError.prototype = Object.create(Error.prototype);
|
|
88
80
|
ExtensibleError.prototype.constructor = ExtensibleError;
|
|
89
81
|
|
|
@@ -100,7 +92,6 @@ Object.defineProperty(ExtensibleError.prototype, 'stack', {
|
|
|
100
92
|
if (this._stack) {
|
|
101
93
|
return this._stack;
|
|
102
94
|
}
|
|
103
|
-
|
|
104
95
|
let stack = this._nativeError.stack;
|
|
105
96
|
if (typeof stack !== 'string') {
|
|
106
97
|
return undefined;
|
|
@@ -110,7 +101,6 @@ Object.defineProperty(ExtensibleError.prototype, 'stack', {
|
|
|
110
101
|
// constructors
|
|
111
102
|
if (this._dropInternalStackFrames) {
|
|
112
103
|
let stackLines = stack.split('\n');
|
|
113
|
-
|
|
114
104
|
let inheritanceDepth = 1;
|
|
115
105
|
let currentPrototype = Object.getPrototypeOf(this);
|
|
116
106
|
while (currentPrototype !== ExtensibleError.prototype) {
|
|
@@ -118,75 +108,59 @@ Object.defineProperty(ExtensibleError.prototype, 'stack', {
|
|
|
118
108
|
inheritanceDepth++;
|
|
119
109
|
}
|
|
120
110
|
stackLines.splice(1, inheritanceDepth);
|
|
121
|
-
|
|
122
111
|
this._stack = stackLines.join('\n');
|
|
123
112
|
} else {
|
|
124
113
|
this._stack = stack;
|
|
125
114
|
}
|
|
126
|
-
|
|
127
115
|
return this._stack;
|
|
128
|
-
}
|
|
116
|
+
}
|
|
129
117
|
});
|
|
130
118
|
|
|
131
119
|
class GenericError extends ExtensibleError {
|
|
132
120
|
constructor(message, params) {
|
|
133
121
|
super(message);
|
|
134
|
-
const {
|
|
135
|
-
|
|
122
|
+
const {
|
|
123
|
+
status = 500,
|
|
124
|
+
...otherParams
|
|
125
|
+
} = params;
|
|
136
126
|
this.name = 'Error';
|
|
137
127
|
this.status = status;
|
|
138
|
-
|
|
139
128
|
this._params = otherParams;
|
|
140
129
|
}
|
|
141
|
-
|
|
142
130
|
get params() {
|
|
143
131
|
return this._params;
|
|
144
132
|
}
|
|
145
133
|
}
|
|
146
134
|
|
|
147
135
|
const DEV = 'development';
|
|
148
|
-
const ENV =
|
|
149
|
-
typeof process !== 'undefined' && process && process.env
|
|
150
|
-
? process.env.NODE_ENV
|
|
151
|
-
: DEV;
|
|
152
|
-
|
|
136
|
+
const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
|
|
153
137
|
const ERROR_EVENTS = {
|
|
154
|
-
ERROR: '@merkur/plugin-error.error'
|
|
138
|
+
ERROR: '@merkur/plugin-error.error'
|
|
155
139
|
};
|
|
156
|
-
|
|
157
140
|
function errorPlugin() {
|
|
158
141
|
return {
|
|
159
142
|
async setup(widget) {
|
|
160
|
-
widget.error = widget.error
|
|
161
|
-
|
|
162
|
-
:
|
|
163
|
-
|
|
164
|
-
message: null,
|
|
165
|
-
};
|
|
166
|
-
|
|
143
|
+
widget.error = widget.error ? widget.error : {
|
|
144
|
+
status: null,
|
|
145
|
+
message: null
|
|
146
|
+
};
|
|
167
147
|
return widget;
|
|
168
148
|
},
|
|
169
149
|
async create(widget) {
|
|
170
150
|
if (ENV === DEV) {
|
|
171
151
|
if (!widget.$in.component) {
|
|
172
|
-
throw new Error(
|
|
173
|
-
'You must install missing plugin: npm i @merkur/plugin-component',
|
|
174
|
-
);
|
|
152
|
+
throw new Error('You must install missing plugin: npm i @merkur/plugin-component');
|
|
175
153
|
}
|
|
176
154
|
if (!widget.$in.eventEmitter) {
|
|
177
|
-
throw new Error(
|
|
178
|
-
'You must install missing plugin: npm i @merkur/plugin-event-emitter',
|
|
179
|
-
);
|
|
155
|
+
throw new Error('You must install missing plugin: npm i @merkur/plugin-event-emitter');
|
|
180
156
|
}
|
|
181
157
|
}
|
|
182
|
-
|
|
183
158
|
core.hookMethod(widget, 'info', infoHook);
|
|
184
159
|
core.hookMethod(widget, 'load', loadHook);
|
|
185
160
|
core.hookMethod(widget, 'mount', mountHook);
|
|
186
161
|
core.hookMethod(widget, 'update', updateHook);
|
|
187
|
-
|
|
188
162
|
return widget;
|
|
189
|
-
}
|
|
163
|
+
}
|
|
190
164
|
};
|
|
191
165
|
}
|
|
192
166
|
|
|
@@ -197,32 +171,24 @@ async function loadHook(widget, originalLoad, ...rest) {
|
|
|
197
171
|
if (widget.error.status) {
|
|
198
172
|
return result;
|
|
199
173
|
}
|
|
200
|
-
|
|
201
174
|
try {
|
|
202
175
|
result = await originalLoad(...rest);
|
|
203
176
|
} catch (error) {
|
|
204
177
|
error.status = error.status || 500;
|
|
205
|
-
|
|
206
178
|
setErrorInfo(widget, error);
|
|
207
|
-
emitError(widget, error);
|
|
208
179
|
}
|
|
209
|
-
|
|
210
180
|
return result;
|
|
211
181
|
}
|
|
212
|
-
|
|
213
182
|
async function infoHook(widget, originalInfo, ...rest) {
|
|
214
183
|
const result = await originalInfo(...rest);
|
|
215
|
-
|
|
216
184
|
return {
|
|
217
185
|
error: widget.error,
|
|
218
|
-
...result
|
|
186
|
+
...result
|
|
219
187
|
};
|
|
220
188
|
}
|
|
221
|
-
|
|
222
189
|
async function mountHook(widget, originalMount, ...rest) {
|
|
223
190
|
return renderContent(widget, originalMount, rest);
|
|
224
191
|
}
|
|
225
|
-
|
|
226
192
|
async function updateHook(widget, originalUpdate, ...rest) {
|
|
227
193
|
return renderContent(widget, originalUpdate, rest);
|
|
228
194
|
}
|
|
@@ -233,19 +199,15 @@ function setErrorInfo(widget, error) {
|
|
|
233
199
|
widget.error.status = error.status;
|
|
234
200
|
widget.error.message = error.message;
|
|
235
201
|
widget.error.url = error.params && error.params.url;
|
|
236
|
-
|
|
237
202
|
if (ENV === DEV) {
|
|
238
203
|
widget.error.stack = error.stack;
|
|
239
204
|
}
|
|
205
|
+
widget.emit(ERROR_EVENTS.ERROR, {
|
|
206
|
+
error
|
|
207
|
+
});
|
|
240
208
|
}
|
|
241
|
-
|
|
242
|
-
function emitError(widget, error) {
|
|
243
|
-
widget.emit(ERROR_EVENTS.ERROR, { error });
|
|
244
|
-
}
|
|
245
|
-
|
|
246
209
|
async function renderContent(widget, method, properties) {
|
|
247
210
|
let result = null;
|
|
248
|
-
|
|
249
211
|
if (widget.error.status) {
|
|
250
212
|
// error was captured in an earlier lifecycle method
|
|
251
213
|
try {
|
|
@@ -267,7 +229,6 @@ async function renderContent(widget, method, properties) {
|
|
|
267
229
|
// save error info
|
|
268
230
|
err.status = err.status || 500;
|
|
269
231
|
setErrorInfo(widget, err);
|
|
270
|
-
emitError(widget, err);
|
|
271
232
|
|
|
272
233
|
// try rendering again
|
|
273
234
|
return renderContent(widget, method, properties);
|
package/lib/index.mjs
CHANGED
|
@@ -16,20 +16,13 @@ import { hookMethod } from '@merkur/core';
|
|
|
16
16
|
* call stack frames are dropped by the JS engine).
|
|
17
17
|
* This flag is enabled by default.
|
|
18
18
|
*/
|
|
19
|
-
function ExtensibleError(
|
|
20
|
-
message,
|
|
21
|
-
dropInternalStackFrames = true,
|
|
22
|
-
) {
|
|
19
|
+
function ExtensibleError(message, dropInternalStackFrames = true) {
|
|
23
20
|
if (!(this instanceof ExtensibleError)) {
|
|
24
21
|
throw new TypeError('Cannot call a class as a function');
|
|
25
22
|
}
|
|
26
23
|
if (this.constructor === ExtensibleError) {
|
|
27
|
-
throw new TypeError(
|
|
28
|
-
'The ExtensibleError is an abstract class and ' +
|
|
29
|
-
'must be extended before it can be instantiated.',
|
|
30
|
-
);
|
|
24
|
+
throw new TypeError('The ExtensibleError is an abstract class and ' + 'must be extended before it can be instantiated.');
|
|
31
25
|
}
|
|
32
|
-
|
|
33
26
|
Error.call(this, message); // super-constructor call;
|
|
34
27
|
|
|
35
28
|
/**
|
|
@@ -81,7 +74,6 @@ function ExtensibleError(
|
|
|
81
74
|
*/
|
|
82
75
|
this._dropInternalStackFrames = dropInternalStackFrames;
|
|
83
76
|
}
|
|
84
|
-
|
|
85
77
|
ExtensibleError.prototype = Object.create(Error.prototype);
|
|
86
78
|
ExtensibleError.prototype.constructor = ExtensibleError;
|
|
87
79
|
|
|
@@ -98,7 +90,6 @@ Object.defineProperty(ExtensibleError.prototype, 'stack', {
|
|
|
98
90
|
if (this._stack) {
|
|
99
91
|
return this._stack;
|
|
100
92
|
}
|
|
101
|
-
|
|
102
93
|
let stack = this._nativeError.stack;
|
|
103
94
|
if (typeof stack !== 'string') {
|
|
104
95
|
return undefined;
|
|
@@ -108,7 +99,6 @@ Object.defineProperty(ExtensibleError.prototype, 'stack', {
|
|
|
108
99
|
// constructors
|
|
109
100
|
if (this._dropInternalStackFrames) {
|
|
110
101
|
let stackLines = stack.split('\n');
|
|
111
|
-
|
|
112
102
|
let inheritanceDepth = 1;
|
|
113
103
|
let currentPrototype = Object.getPrototypeOf(this);
|
|
114
104
|
while (currentPrototype !== ExtensibleError.prototype) {
|
|
@@ -116,75 +106,59 @@ Object.defineProperty(ExtensibleError.prototype, 'stack', {
|
|
|
116
106
|
inheritanceDepth++;
|
|
117
107
|
}
|
|
118
108
|
stackLines.splice(1, inheritanceDepth);
|
|
119
|
-
|
|
120
109
|
this._stack = stackLines.join('\n');
|
|
121
110
|
} else {
|
|
122
111
|
this._stack = stack;
|
|
123
112
|
}
|
|
124
|
-
|
|
125
113
|
return this._stack;
|
|
126
|
-
}
|
|
114
|
+
}
|
|
127
115
|
});
|
|
128
116
|
|
|
129
117
|
class GenericError extends ExtensibleError {
|
|
130
118
|
constructor(message, params) {
|
|
131
119
|
super(message);
|
|
132
|
-
const {
|
|
133
|
-
|
|
120
|
+
const {
|
|
121
|
+
status = 500,
|
|
122
|
+
...otherParams
|
|
123
|
+
} = params;
|
|
134
124
|
this.name = 'Error';
|
|
135
125
|
this.status = status;
|
|
136
|
-
|
|
137
126
|
this._params = otherParams;
|
|
138
127
|
}
|
|
139
|
-
|
|
140
128
|
get params() {
|
|
141
129
|
return this._params;
|
|
142
130
|
}
|
|
143
131
|
}
|
|
144
132
|
|
|
145
133
|
const DEV = 'development';
|
|
146
|
-
const ENV =
|
|
147
|
-
typeof process !== 'undefined' && process && process.env
|
|
148
|
-
? process.env.NODE_ENV
|
|
149
|
-
: DEV;
|
|
150
|
-
|
|
134
|
+
const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
|
|
151
135
|
const ERROR_EVENTS = {
|
|
152
|
-
ERROR: '@merkur/plugin-error.error'
|
|
136
|
+
ERROR: '@merkur/plugin-error.error'
|
|
153
137
|
};
|
|
154
|
-
|
|
155
138
|
function errorPlugin() {
|
|
156
139
|
return {
|
|
157
140
|
async setup(widget) {
|
|
158
|
-
widget.error = widget.error
|
|
159
|
-
|
|
160
|
-
:
|
|
161
|
-
|
|
162
|
-
message: null,
|
|
163
|
-
};
|
|
164
|
-
|
|
141
|
+
widget.error = widget.error ? widget.error : {
|
|
142
|
+
status: null,
|
|
143
|
+
message: null
|
|
144
|
+
};
|
|
165
145
|
return widget;
|
|
166
146
|
},
|
|
167
147
|
async create(widget) {
|
|
168
148
|
if (ENV === DEV) {
|
|
169
149
|
if (!widget.$in.component) {
|
|
170
|
-
throw new Error(
|
|
171
|
-
'You must install missing plugin: npm i @merkur/plugin-component',
|
|
172
|
-
);
|
|
150
|
+
throw new Error('You must install missing plugin: npm i @merkur/plugin-component');
|
|
173
151
|
}
|
|
174
152
|
if (!widget.$in.eventEmitter) {
|
|
175
|
-
throw new Error(
|
|
176
|
-
'You must install missing plugin: npm i @merkur/plugin-event-emitter',
|
|
177
|
-
);
|
|
153
|
+
throw new Error('You must install missing plugin: npm i @merkur/plugin-event-emitter');
|
|
178
154
|
}
|
|
179
155
|
}
|
|
180
|
-
|
|
181
156
|
hookMethod(widget, 'info', infoHook);
|
|
182
157
|
hookMethod(widget, 'load', loadHook);
|
|
183
158
|
hookMethod(widget, 'mount', mountHook);
|
|
184
159
|
hookMethod(widget, 'update', updateHook);
|
|
185
|
-
|
|
186
160
|
return widget;
|
|
187
|
-
}
|
|
161
|
+
}
|
|
188
162
|
};
|
|
189
163
|
}
|
|
190
164
|
|
|
@@ -195,32 +169,24 @@ async function loadHook(widget, originalLoad, ...rest) {
|
|
|
195
169
|
if (widget.error.status) {
|
|
196
170
|
return result;
|
|
197
171
|
}
|
|
198
|
-
|
|
199
172
|
try {
|
|
200
173
|
result = await originalLoad(...rest);
|
|
201
174
|
} catch (error) {
|
|
202
175
|
error.status = error.status || 500;
|
|
203
|
-
|
|
204
176
|
setErrorInfo(widget, error);
|
|
205
|
-
emitError(widget, error);
|
|
206
177
|
}
|
|
207
|
-
|
|
208
178
|
return result;
|
|
209
179
|
}
|
|
210
|
-
|
|
211
180
|
async function infoHook(widget, originalInfo, ...rest) {
|
|
212
181
|
const result = await originalInfo(...rest);
|
|
213
|
-
|
|
214
182
|
return {
|
|
215
183
|
error: widget.error,
|
|
216
|
-
...result
|
|
184
|
+
...result
|
|
217
185
|
};
|
|
218
186
|
}
|
|
219
|
-
|
|
220
187
|
async function mountHook(widget, originalMount, ...rest) {
|
|
221
188
|
return renderContent(widget, originalMount, rest);
|
|
222
189
|
}
|
|
223
|
-
|
|
224
190
|
async function updateHook(widget, originalUpdate, ...rest) {
|
|
225
191
|
return renderContent(widget, originalUpdate, rest);
|
|
226
192
|
}
|
|
@@ -231,19 +197,15 @@ function setErrorInfo(widget, error) {
|
|
|
231
197
|
widget.error.status = error.status;
|
|
232
198
|
widget.error.message = error.message;
|
|
233
199
|
widget.error.url = error.params && error.params.url;
|
|
234
|
-
|
|
235
200
|
if (ENV === DEV) {
|
|
236
201
|
widget.error.stack = error.stack;
|
|
237
202
|
}
|
|
203
|
+
widget.emit(ERROR_EVENTS.ERROR, {
|
|
204
|
+
error
|
|
205
|
+
});
|
|
238
206
|
}
|
|
239
|
-
|
|
240
|
-
function emitError(widget, error) {
|
|
241
|
-
widget.emit(ERROR_EVENTS.ERROR, { error });
|
|
242
|
-
}
|
|
243
|
-
|
|
244
207
|
async function renderContent(widget, method, properties) {
|
|
245
208
|
let result = null;
|
|
246
|
-
|
|
247
209
|
if (widget.error.status) {
|
|
248
210
|
// error was captured in an earlier lifecycle method
|
|
249
211
|
try {
|
|
@@ -265,7 +227,6 @@ async function renderContent(widget, method, properties) {
|
|
|
265
227
|
// save error info
|
|
266
228
|
err.status = err.status || 500;
|
|
267
229
|
setErrorInfo(widget, err);
|
|
268
|
-
emitError(widget, err);
|
|
269
230
|
|
|
270
231
|
// try rendering again
|
|
271
232
|
return renderContent(widget, method, properties);
|
package/lib/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function r(t){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},r(t)}!function(r,t){if("function"==typeof define&&define.amd)define("@merkur/plugin-error",["exports","@merkur/core"],t);else if("undefined"!=typeof exports)t(exports,require("@merkur/core"));else{var e={exports:{}};t(e.exports,r.Merkur.Core),r.Merkur=r.Merkur||{},r.Merkur.Plugin=r.Merkur.Plugin||{},r.Merkur.Plugin.Error=e.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(t,e){Object.defineProperty(t,"__esModule",{value:!0}),t.GenericError=t.ERROR_EVENTS=void 0,t.errorPlugin=function(){return{setup:function(r){return s((function*(){return r.error=r.error?r.error:{status:null,message:null},r}))()},create:function(r){return s((function*(){if(d===v){if(!r.$in.component)throw new Error("You must install missing plugin: npm i @merkur/plugin-component");if(!r.$in.eventEmitter)throw new Error("You must install missing plugin: npm i @merkur/plugin-event-emitter")}return(0,e.hookMethod)(r,"info",E),(0,e.hookMethod)(r,"load",O),(0,e.hookMethod)(r,"mount",P),(0,e.hookMethod)(r,"update",_),r}))()}}},t.renderContent=
|
|
1
|
+
function r(t){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},r(t)}!function(r,t){if("function"==typeof define&&define.amd)define("@merkur/plugin-error",["exports","@merkur/core"],t);else if("undefined"!=typeof exports)t(exports,require("@merkur/core"));else{var e={exports:{}};t(e.exports,r.Merkur.Core),r.Merkur=r.Merkur||{},r.Merkur.Plugin=r.Merkur.Plugin||{},r.Merkur.Plugin.Error=e.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(t,e){Object.defineProperty(t,"__esModule",{value:!0}),t.GenericError=t.ERROR_EVENTS=void 0,t.errorPlugin=function(){return{setup:function(r){return s((function*(){return r.error=r.error?r.error:{status:null,message:null},r}))()},create:function(r){return s((function*(){if(d===v){if(!r.$in.component)throw new Error("You must install missing plugin: npm i @merkur/plugin-component");if(!r.$in.eventEmitter)throw new Error("You must install missing plugin: npm i @merkur/plugin-event-emitter")}return(0,e.hookMethod)(r,"info",E),(0,e.hookMethod)(r,"load",O),(0,e.hookMethod)(r,"mount",P),(0,e.hookMethod)(r,"update",_),r}))()}}},t.renderContent=x,t.setErrorInfo=R;var n=["status"];function o(r){return function(r){if(Array.isArray(r))return i(r)}(r)||function(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}(r)||function(r,t){if(r){if("string"==typeof r)return i(r,t);var e={}.toString.call(r).slice(8,-1);return"Object"===e&&r.constructor&&(e=r.constructor.name),"Map"===e||"Set"===e?Array.from(r):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?i(r,t):void 0}}(r)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(r,t){(null==t||t>r.length)&&(t=r.length);for(var e=0,n=Array(t);e<t;e++)n[e]=r[e];return n}function u(r,t){var e=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),e.push.apply(e,n)}return e}function a(r,t,e){return(t=l(t))in r?Object.defineProperty(r,t,{value:e,enumerable:!0,configurable:!0,writable:!0}):r[t]=e,r}function c(r,t,e,n,o,i,u){try{var a=r[i](u),c=a.value}catch(r){return void e(r)}a.done?t(c):Promise.resolve(c).then(n,o)}function s(r){return function(){var t=this,e=arguments;return new Promise((function(n,o){var i=r.apply(t,e);function u(r){c(i,n,o,u,a,"next",r)}function a(r){c(i,n,o,u,a,"throw",r)}u(void 0)}))}}function f(r,t){for(var e=0;e<t.length;e++){var n=t[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(r,l(n.key),n)}}function l(t){var e=function(t,e){if("object"!=r(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var o=n.call(t,e||"default");if("object"!=r(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==r(e)?e:e+""}function p(t,e,n){return e=h(e),function(t,e){if(e&&("object"==r(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(r){if(void 0===r)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return r}(t)}(t,y()?Reflect.construct(e,n||[],h(t).constructor):e.apply(t,n))}function y(){try{var r=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(r){}return(y=function(){return!!r})()}function h(r){return h=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(r){return r.__proto__||Object.getPrototypeOf(r)},h(r)}function b(r,t){return b=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(r,t){return r.__proto__=t,r},b(r,t)}function m(r){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(!(this instanceof m))throw new TypeError("Cannot call a class as a function");if(this.constructor===m)throw new TypeError("The ExtensibleError is an abstract class and must be extended before it can be instantiated.");Error.call(this,r),this.name=this.constructor.name,this.message=r,this._nativeError=new Error(r),this._nativeError.name=this.name,this._nativeError.columnNumber&&(this.lineNumber=this._nativeError.lineNumber,this.columnNumber=this._nativeError.columnNumber,this.fileName=this._nativeError.fileName),this._stack=null,this._dropInternalStackFrames=t}m.prototype=Object.create(Error.prototype),m.prototype.constructor=m,Object.defineProperty(m.prototype,"stack",{configurable:!0,enumerable:!1,get:function(){if(this._stack)return this._stack;var r=this._nativeError.stack;if("string"==typeof r){if(this._dropInternalStackFrames){for(var t=r.split("\n"),e=1,n=Object.getPrototypeOf(this);n!==m.prototype;)n=Object.getPrototypeOf(n),e++;t.splice(1,e),this._stack=t.join("\n")}else this._stack=r;return this._stack}}});t.GenericError=function(r){function t(r,e){var o;!function(r,t){if(!(r instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t),o=p(this,t,[r]);var i=e.status,u=void 0===i?500:i,a=function(r,t){if(null==r)return{};var e,n,o=function(r,t){if(null==r)return{};var e={};for(var n in r)if({}.hasOwnProperty.call(r,n)){if(-1!==t.indexOf(n))continue;e[n]=r[n]}return e}(r,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(r);for(n=0;n<i.length;n++)e=i[n],-1===t.indexOf(e)&&{}.propertyIsEnumerable.call(r,e)&&(o[e]=r[e])}return o}(e,n);return o.name="Error",o.status=u,o._params=a,o}return function(r,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");r.prototype=Object.create(t&&t.prototype,{constructor:{value:r,writable:!0,configurable:!0}}),Object.defineProperty(r,"prototype",{writable:!1}),t&&b(r,t)}(t,r),e=t,(o=[{key:"params",get:function(){return this._params}}])&&f(e.prototype,o),i&&f(e,i),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,o,i}(m);var v="development",d="undefined"!=typeof process&&process&&process.env?process.env.NODE_ENV:v,g=t.ERROR_EVENTS={ERROR:"@merkur/plugin-error.error"};function O(r,t){return w.apply(this,arguments)}function w(){return w=s((function*(r,t){var e={};if(r.error.status)return e;try{for(var n=arguments.length,o=new Array(n>2?n-2:0),i=2;i<n;i++)o[i-2]=arguments[i];e=yield t.apply(void 0,o)}catch(t){t.status=t.status||500,R(r,t)}return e})),w.apply(this,arguments)}function E(r,t){return j.apply(this,arguments)}function j(){return j=s((function*(r,t){for(var e=arguments.length,n=new Array(e>2?e-2:0),o=2;o<e;o++)n[o-2]=arguments[o];var i=yield t.apply(void 0,n);return function(r){for(var t=1;t<arguments.length;t++){var e=null!=arguments[t]?arguments[t]:{};t%2?u(Object(e),!0).forEach((function(t){a(r,t,e[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(e)):u(Object(e)).forEach((function(t){Object.defineProperty(r,t,Object.getOwnPropertyDescriptor(e,t))}))}return r}({error:r.error},i)})),j.apply(this,arguments)}function P(r,t){return k.apply(this,arguments)}function k(){return k=s((function*(r,t){for(var e=arguments.length,n=new Array(e>2?e-2:0),o=2;o<e;o++)n[o-2]=arguments[o];return x(r,t,n)})),k.apply(this,arguments)}function _(r,t){return S.apply(this,arguments)}function S(){return S=s((function*(r,t){for(var e=arguments.length,n=new Array(e>2?e-2:0),o=2;o<e;o++)n[o-2]=arguments[o];return x(r,t,n)})),S.apply(this,arguments)}function R(r,t){r.error.status=t.status,r.error.message=t.message,r.error.url=t.params&&t.params.url,d===v&&(r.error.stack=t.stack),r.emit(g.ERROR,{error:t})}function x(r,t,e){return M.apply(this,arguments)}function M(){return(M=s((function*(r,t,e){if(r.error.status)try{return yield t.apply(void 0,o(e))}catch(r){return""}try{return yield t.apply(void 0,o(e))}catch(n){return n.status=n.status||500,R(r,n),x(r,t,e)}}))).apply(this,arguments)}}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merkur/plugin-error",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.39.0",
|
|
4
4
|
"description": "Merkur plugin for error handling.",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"module": "lib/index",
|
|
@@ -27,8 +27,7 @@
|
|
|
27
27
|
"preversion": "npm test",
|
|
28
28
|
"test": "jest --no-watchman -c ./jest.config.js",
|
|
29
29
|
"test:es:version": "es-check es11 ./lib/index.mjs --module && es-check es9 ./lib/index.es9.mjs --module && es-check es9 ./lib/index.es9.cjs --module",
|
|
30
|
-
"build": "rollup -c rollup.config.mjs"
|
|
31
|
-
"prepare": "npm run build"
|
|
30
|
+
"build": "rollup -c rollup.config.mjs"
|
|
32
31
|
},
|
|
33
32
|
"repository": {
|
|
34
33
|
"type": "git",
|
|
@@ -50,12 +49,12 @@
|
|
|
50
49
|
},
|
|
51
50
|
"homepage": "https://merkur.js.org/",
|
|
52
51
|
"devDependencies": {
|
|
53
|
-
"@merkur/core": "^0.
|
|
54
|
-
"@merkur/plugin-component": "^0.
|
|
52
|
+
"@merkur/core": "^0.39.0",
|
|
53
|
+
"@merkur/plugin-component": "^0.39.0"
|
|
55
54
|
},
|
|
56
55
|
"peerDependencies": {
|
|
57
56
|
"@merkur/core": "*",
|
|
58
57
|
"@merkur/plugin-component": "*"
|
|
59
58
|
},
|
|
60
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "8ad2c8b26246850ce6289502a8b05e882f80ce31"
|
|
61
60
|
}
|
package/server/index.js
CHANGED
|
@@ -1,21 +1,3 @@
|
|
|
1
|
-
//**** CALLBACK DEFINITIONS ****//
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* playgroundErrorMiddleware error render function. This is used when rendering playground fails. It's recommended to either mute this for production, or sanitize the output thoroughly.
|
|
5
|
-
*
|
|
6
|
-
* @callback playgroundErrorRenderer
|
|
7
|
-
* @param {string} message
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Function to render the playground page normally (e.g. a compiled EJS template).
|
|
12
|
-
*
|
|
13
|
-
* @callback playgroundRenderer
|
|
14
|
-
* @param {object} properties
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
//**** ****//
|
|
18
|
-
|
|
19
1
|
const DEV = 'development';
|
|
20
2
|
const ENV =
|
|
21
3
|
typeof process !== 'undefined' && process && process.env
|
|
@@ -63,54 +45,7 @@ function logErrorMiddleware() {
|
|
|
63
45
|
};
|
|
64
46
|
}
|
|
65
47
|
|
|
66
|
-
/**
|
|
67
|
-
* Express middleware that attempts to render a widget that has been returned with a non-OK HTTP status.
|
|
68
|
-
*
|
|
69
|
-
* @param {object} config
|
|
70
|
-
* @param {playgroundRenderer} config.renderPlayground
|
|
71
|
-
* @param {string} config.containerSelector
|
|
72
|
-
* @returns {function}
|
|
73
|
-
*/
|
|
74
|
-
// eslint-disable-next-line no-unused-vars
|
|
75
|
-
function playgroundErrorMiddleware({
|
|
76
|
-
renderPlayground,
|
|
77
|
-
containerSelector,
|
|
78
|
-
} = {}) {
|
|
79
|
-
//eslint-disable-next-line no-unused-vars
|
|
80
|
-
return (error, req, res, next) => {
|
|
81
|
-
// error handling for playground page
|
|
82
|
-
let output = '';
|
|
83
|
-
let errorStatus = error.status || 500;
|
|
84
|
-
|
|
85
|
-
const widgetProperties = error?.response?.body;
|
|
86
|
-
|
|
87
|
-
if (!widgetProperties) {
|
|
88
|
-
next(error);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
errorStatus = widgetProperties?.error?.status || 500;
|
|
93
|
-
const { html } = widgetProperties;
|
|
94
|
-
if (typeof html === 'undefined') {
|
|
95
|
-
next(error);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
delete widgetProperties.html;
|
|
101
|
-
widgetProperties.containerSelector = `.${containerSelector}`;
|
|
102
|
-
|
|
103
|
-
output = renderPlayground({ widgetProperties, html, containerSelector });
|
|
104
|
-
res.status(errorStatus).send(output);
|
|
105
|
-
} catch (e) {
|
|
106
|
-
console.error(e);
|
|
107
|
-
next(widgetProperties.error ?? e);
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
48
|
module.exports = {
|
|
113
49
|
logErrorMiddleware,
|
|
114
50
|
apiErrorMiddleware,
|
|
115
|
-
playgroundErrorMiddleware,
|
|
116
51
|
};
|