@weave-js/core 0.12.1 → 0.13.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/ExtendableError.js +2 -2
- package/lib/errors.js +51 -41
- package/lib/middlewares/error-handler/index.js +2 -2
- package/lib/runtime/initServiceManager.js +6 -1
- package/lib/transport/errorPayloadFactory.js +0 -1
- package/lib/transport/messageHandlers.js +9 -16
- package/lib/utils/restoreError.js +56 -4
- package/package.json +4 -4
package/lib/ExtendableError.js
CHANGED
package/lib/errors.js
CHANGED
|
@@ -4,25 +4,34 @@
|
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
const { defaultsDeep } = require('@weave-js/utils');
|
|
7
8
|
const { ExtendableError } = require('./ExtendableError');
|
|
8
9
|
|
|
9
10
|
class WeaveError extends ExtendableError {
|
|
10
|
-
constructor (message, code, type, data
|
|
11
|
-
|
|
11
|
+
constructor (message, options) { // code, type, data
|
|
12
|
+
options = defaultsDeep(
|
|
13
|
+
options,
|
|
14
|
+
{
|
|
15
|
+
code: 'WEAVE_ERROR',
|
|
16
|
+
retryable: false
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
super(message, options);
|
|
12
21
|
this.name = this.constructor.name;
|
|
13
|
-
this.code = code ||
|
|
14
|
-
this.
|
|
15
|
-
this.data = data;
|
|
22
|
+
this.code = options.code || 'WEAVE_ERROR';
|
|
23
|
+
this.data = options.data;
|
|
16
24
|
this.retryable = false;
|
|
25
|
+
|
|
26
|
+
if (options.statusCode) {
|
|
27
|
+
this.statusCode = options.statusCode;
|
|
28
|
+
}
|
|
17
29
|
}
|
|
18
30
|
}
|
|
19
31
|
|
|
20
32
|
class WeaveRetryableError extends WeaveError {
|
|
21
|
-
constructor (message, code
|
|
22
|
-
super(message);
|
|
23
|
-
this.code = code || 500;
|
|
24
|
-
this.type = type;
|
|
25
|
-
this.data = data;
|
|
33
|
+
constructor (message, options = { code: 'WEAVE_RETRYABLE_ERROR', retryable: true }) {
|
|
34
|
+
super(message, options);
|
|
26
35
|
this.retryable = true;
|
|
27
36
|
}
|
|
28
37
|
}
|
|
@@ -39,11 +48,11 @@ class WeaveServiceNotFoundError extends WeaveRetryableError {
|
|
|
39
48
|
message = 'Service not found.';
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
super(message,
|
|
51
|
+
super(message, { code: 'WEAVE_SERVICE_NOT_FOUND_ERROR', data });
|
|
43
52
|
}
|
|
44
53
|
}
|
|
45
54
|
|
|
46
|
-
class WeaveServiceNotAvailableError extends WeaveRetryableError {
|
|
55
|
+
class WeaveServiceNotAvailableError extends WeaveRetryableError { // 503
|
|
47
56
|
constructor (data = {}) {
|
|
48
57
|
let message;
|
|
49
58
|
if (data.nodeId) {
|
|
@@ -54,67 +63,69 @@ class WeaveServiceNotAvailableError extends WeaveRetryableError {
|
|
|
54
63
|
message = 'Service not available.';
|
|
55
64
|
}
|
|
56
65
|
|
|
57
|
-
super(message,
|
|
66
|
+
super(message, { code: 'WEAVE_SERVICE_NOT_AVAILABLE_ERROR', data });
|
|
58
67
|
}
|
|
59
68
|
}
|
|
60
69
|
|
|
61
|
-
class WeaveRequestTimeoutError extends WeaveRetryableError {
|
|
70
|
+
class WeaveRequestTimeoutError extends WeaveRetryableError { // 504
|
|
62
71
|
constructor (actionName, nodeId, timeout) {
|
|
63
|
-
const
|
|
64
|
-
super(message, 504, 'WEAVE_REQUEST_TIMEOUT_ERROR', {
|
|
72
|
+
const data = {
|
|
65
73
|
actionName,
|
|
66
74
|
nodeId,
|
|
67
75
|
timeout
|
|
68
|
-
}
|
|
69
|
-
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const message = `Action ${actionName} timed out node ${nodeId || '<local>'}.`;
|
|
79
|
+
super(message, { code: 'WEAVE_REQUEST_TIMEOUT_ERROR', data });
|
|
70
80
|
}
|
|
71
81
|
}
|
|
72
82
|
|
|
73
|
-
class WeaveParameterValidationError extends WeaveError {
|
|
83
|
+
class WeaveParameterValidationError extends WeaveError { // 422
|
|
74
84
|
constructor (message, data) {
|
|
75
|
-
super(message,
|
|
85
|
+
super(message, { code: 'WEAVE_PARAMETER_VALIDATION_ERROR', data });
|
|
76
86
|
}
|
|
77
87
|
}
|
|
78
88
|
|
|
79
89
|
class WeaveBrokerOptionsError extends WeaveError {
|
|
80
90
|
constructor (message, data) {
|
|
81
|
-
super(
|
|
91
|
+
super(
|
|
92
|
+
message,
|
|
93
|
+
{ code: 'WEAVE_BROKER_OPTIONS_ERROR', data }
|
|
94
|
+
);
|
|
82
95
|
}
|
|
83
96
|
}
|
|
84
97
|
|
|
85
|
-
class WeaveQueueSizeExceededError extends WeaveError {
|
|
98
|
+
class WeaveQueueSizeExceededError extends WeaveError { // 429
|
|
86
99
|
constructor (data) {
|
|
87
|
-
super(
|
|
100
|
+
super(
|
|
101
|
+
'Queue size limit was exceeded. Request rejected.',
|
|
102
|
+
{ code: 'WEAVE_QUEUE_SIZE_EXCEEDED_ERROR', data }
|
|
103
|
+
);
|
|
88
104
|
}
|
|
89
105
|
}
|
|
90
106
|
|
|
91
107
|
class WeaveMaxCallLevelError extends WeaveError {
|
|
92
108
|
constructor (data) {
|
|
93
|
-
super(
|
|
109
|
+
super(
|
|
110
|
+
`Request level has reached the limit ${data.maxCallLevel} on node "${data.nodeId}".`,
|
|
111
|
+
{ code: 'WEAVE_MAX_CALL_LEVEL_ERROR', data }
|
|
112
|
+
);
|
|
94
113
|
}
|
|
95
114
|
}
|
|
96
115
|
|
|
97
116
|
class WeaveGracefulStopTimeoutError extends WeaveError {
|
|
98
117
|
constructor (service) {
|
|
99
|
-
|
|
118
|
+
const data = {
|
|
100
119
|
name: service.name,
|
|
101
120
|
version: service.version
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const restoreError = error => {
|
|
107
|
-
const ErrorClass = module.exports[error.name];
|
|
121
|
+
};
|
|
108
122
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
// case 'WeaveParameterValidationError':
|
|
114
|
-
// return new ErrorClass(error.message, error.data)
|
|
115
|
-
}
|
|
123
|
+
super(
|
|
124
|
+
`Unable to stop service "${service.name}"`,
|
|
125
|
+
{ code: 'WEAVE_GRACEFUL_STOP_TIMEOUT', data }
|
|
126
|
+
);
|
|
116
127
|
}
|
|
117
|
-
}
|
|
128
|
+
}
|
|
118
129
|
|
|
119
130
|
module.exports = {
|
|
120
131
|
WeaveBrokerOptionsError,
|
|
@@ -126,6 +137,5 @@ module.exports = {
|
|
|
126
137
|
WeaveRetryableError,
|
|
127
138
|
WeaveServiceNotAvailableError,
|
|
128
139
|
WeaveServiceNotFoundError,
|
|
129
|
-
WeaveGracefulStopTimeoutError
|
|
130
|
-
restoreError
|
|
140
|
+
WeaveGracefulStopTimeoutError
|
|
131
141
|
};
|
|
@@ -12,7 +12,7 @@ module.exports = (runtime) => {
|
|
|
12
12
|
return handler(context, serviceInjections)
|
|
13
13
|
.catch((error) => {
|
|
14
14
|
if (!(error instanceof Error)) {
|
|
15
|
-
error = new WeaveError(error
|
|
15
|
+
error = new WeaveError(error);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
if (runtime.nodeId !== context.nodeId) {
|
|
@@ -36,7 +36,7 @@ module.exports = (runtime) => {
|
|
|
36
36
|
return handler(context, serviceInjections)
|
|
37
37
|
.catch((error) => {
|
|
38
38
|
if (!(error instanceof Error)) {
|
|
39
|
-
error = new WeaveError(error,
|
|
39
|
+
error = new WeaveError(error.message, { cause: error });
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
if (runtime.nodeId !== context.nodeId) {
|
|
@@ -93,7 +93,12 @@ exports.initServiceManager = (runtime) => {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
if (timeout && (Date.now() - startTimestamp) > timeout) {
|
|
96
|
-
return reject(new WeaveError('The waiting of the services is interrupted due to a timeout.',
|
|
96
|
+
return reject(new WeaveError('The waiting of the services is interrupted due to a timeout.', {
|
|
97
|
+
code: 'WAIT_FOR_SERVICE',
|
|
98
|
+
data: {
|
|
99
|
+
services: serviceNames
|
|
100
|
+
}
|
|
101
|
+
}));
|
|
97
102
|
}
|
|
98
103
|
|
|
99
104
|
options.waitForServiceInterval = setTimeout(serviceCheck, interval);
|
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
const { InboundTransformStream } = require('./InboundTransformStream');
|
|
8
|
-
const { WeaveError
|
|
8
|
+
const { WeaveError } = require('../errors');
|
|
9
9
|
const { createContext } = require('../broker/context');
|
|
10
10
|
const { createMessage } = require('./createMessage');
|
|
11
11
|
const MessageTypes = require('./messageTypes');
|
|
12
|
+
const { restoreError } = require('../utils/restoreError');
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* @typedef {import('../types').Transport} Transport
|
|
@@ -291,23 +292,10 @@ module.exports = (runtime, transport) => {
|
|
|
291
292
|
|
|
292
293
|
// Response is an error
|
|
293
294
|
if (!payload.success) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
if (!error) {
|
|
297
|
-
error = new Error(payload.error.message);
|
|
298
|
-
error.name = payload.error.name;
|
|
299
|
-
error.code = payload.error.code;
|
|
300
|
-
error.type = payload.error.type;
|
|
301
|
-
error.data = payload.error.data;
|
|
302
|
-
}
|
|
295
|
+
const error = restoreError(payload.error);
|
|
303
296
|
|
|
304
|
-
error.retryable = payload.error.retryable;
|
|
305
297
|
error.nodeId = error.nodeId || payload.sender;
|
|
306
298
|
|
|
307
|
-
if (payload.error.stack) {
|
|
308
|
-
error.stack = payload.error.stack;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
299
|
request.reject(error);
|
|
312
300
|
}
|
|
313
301
|
|
|
@@ -398,6 +386,7 @@ module.exports = (runtime, transport) => {
|
|
|
398
386
|
const onHeartbeat = (payload) => {
|
|
399
387
|
transport.log.verbose(`Heartbeat from ${payload.sender}`);
|
|
400
388
|
const node = registry.nodeCollection.get(payload.sender);
|
|
389
|
+
|
|
401
390
|
// if node is unknown then request a node info message.
|
|
402
391
|
if (node) {
|
|
403
392
|
if (!node.isAvailable) {
|
|
@@ -510,7 +499,11 @@ module.exports = (runtime, transport) => {
|
|
|
510
499
|
|
|
511
500
|
return true;
|
|
512
501
|
} catch (error) {
|
|
513
|
-
transport.log.error(error, data);
|
|
502
|
+
transport.log.error(error, type, data);
|
|
503
|
+
|
|
504
|
+
runtime.eventBus.broadcastLocal('$transport.error', {
|
|
505
|
+
error
|
|
506
|
+
});
|
|
514
507
|
}
|
|
515
508
|
return false;
|
|
516
509
|
};
|
|
@@ -1,12 +1,64 @@
|
|
|
1
1
|
const errors = require('../errors');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
const ErrorClass = errors[
|
|
3
|
+
const restoreError = (errorPayload) => {
|
|
4
|
+
const ErrorClass = errors[errorPayload.name];
|
|
5
|
+
let error;
|
|
5
6
|
|
|
6
7
|
if (ErrorClass) {
|
|
7
|
-
switch (
|
|
8
|
+
switch (errorPayload.name) {
|
|
8
9
|
case 'WeaveError':
|
|
9
|
-
|
|
10
|
+
case 'WeaveRetryableError': {
|
|
11
|
+
const { message, ...options } = errorPayload;
|
|
12
|
+
error = new ErrorClass(message, options);
|
|
13
|
+
break;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
case 'WeaveParameterValidationError':
|
|
17
|
+
case 'WeaveBrokerOptionsError': {
|
|
18
|
+
const { message, data } = errorPayload;
|
|
19
|
+
error = new ErrorClass(message, data);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case 'WeaveServiceNotFoundError':
|
|
23
|
+
case 'WeaveServiceNotAvailableError':
|
|
24
|
+
case 'WeaveQueueSizeExceededError':
|
|
25
|
+
case 'WeaveMaxCallLevelError': {
|
|
26
|
+
const { data } = errorPayload;
|
|
27
|
+
error = new ErrorClass(data);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case 'WeaveRequestTimeoutError': {
|
|
31
|
+
const { data } = errorPayload;
|
|
32
|
+
error = new ErrorClass(data.actionName, data.nodeId, data.timeout);
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
case 'WeaveGracefulStopTimeoutError': {
|
|
36
|
+
const { data } = errorPayload;
|
|
37
|
+
error = new ErrorClass(data.service);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
error = new Error(errorPayload.message);
|
|
43
|
+
|
|
44
|
+
error.name = errorPayload.name;
|
|
45
|
+
|
|
46
|
+
error.nodeId = errorPayload.nodeId;
|
|
47
|
+
|
|
48
|
+
if (errorPayload.code) {
|
|
49
|
+
error.code = errorPayload.code;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (errorPayload.data) {
|
|
53
|
+
error.data = errorPayload.data;
|
|
10
54
|
}
|
|
11
55
|
}
|
|
56
|
+
|
|
57
|
+
if (errorPayload.stack) {
|
|
58
|
+
error.stack = errorPayload.stack;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return error;
|
|
12
62
|
};
|
|
63
|
+
|
|
64
|
+
module.exports = { restoreError };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weave-js/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "The core package of weave",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Weave",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"license": "MIT",
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@weave-js/errors": "^0.9.1",
|
|
43
|
-
"@weave-js/utils": "^0.
|
|
44
|
-
"@weave-js/validator": "^0.
|
|
43
|
+
"@weave-js/utils": "^0.12.0",
|
|
44
|
+
"@weave-js/validator": "^0.13.0",
|
|
45
45
|
"eventemitter2": "^6.4.5",
|
|
46
46
|
"glob": "^7.2.0"
|
|
47
47
|
},
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"lib": "lib",
|
|
50
50
|
"test": "test"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "6772c8bfb00be1da32c17cff621a8abcd98a08fc"
|
|
53
53
|
}
|