@lvce-editor/json-rpc 6.0.0 → 6.2.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/dist/index.js +39 -15
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -120,6 +120,19 @@ const constructError = (message, type, name) => {
|
|
|
120
120
|
return new ErrorConstructor(message);
|
|
121
121
|
};
|
|
122
122
|
|
|
123
|
+
const joinLines = lines => {
|
|
124
|
+
return lines.join(NewLine);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const splitLines = lines => {
|
|
128
|
+
return lines.split(NewLine);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const getCurrentStack = () => {
|
|
132
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(2));
|
|
133
|
+
return currentStack;
|
|
134
|
+
};
|
|
135
|
+
|
|
123
136
|
const getNewLineIndex = (string, startIndex = undefined) => {
|
|
124
137
|
return string.indexOf(NewLine, startIndex);
|
|
125
138
|
};
|
|
@@ -132,22 +145,17 @@ const getParentStack = error => {
|
|
|
132
145
|
return parentStack;
|
|
133
146
|
};
|
|
134
147
|
|
|
135
|
-
const joinLines = lines => {
|
|
136
|
-
return lines.join(NewLine);
|
|
137
|
-
};
|
|
138
|
-
|
|
139
148
|
const MethodNotFound = -32601;
|
|
140
149
|
const Custom = -32001;
|
|
141
150
|
|
|
142
|
-
const splitLines = lines => {
|
|
143
|
-
return lines.split(NewLine);
|
|
144
|
-
};
|
|
145
|
-
|
|
146
151
|
const restoreJsonRpcError = error => {
|
|
152
|
+
const currentStack = getCurrentStack();
|
|
147
153
|
if (error && error instanceof Error) {
|
|
154
|
+
if (typeof error.stack === 'string') {
|
|
155
|
+
error.stack = error.stack + NewLine + currentStack;
|
|
156
|
+
}
|
|
148
157
|
return error;
|
|
149
158
|
}
|
|
150
|
-
const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
|
|
151
159
|
if (error && error.code && error.code === MethodNotFound) {
|
|
152
160
|
const restoredError = new JsonRpcError(error.message);
|
|
153
161
|
const parentStack = getParentStack(error);
|
|
@@ -266,19 +274,19 @@ const getErrorProperty = (error, prettyError) => {
|
|
|
266
274
|
};
|
|
267
275
|
};
|
|
268
276
|
|
|
269
|
-
const create$1 = (
|
|
277
|
+
const create$1 = (id, error) => {
|
|
270
278
|
return {
|
|
271
279
|
jsonrpc: Two,
|
|
272
|
-
id
|
|
280
|
+
id,
|
|
273
281
|
error
|
|
274
282
|
};
|
|
275
283
|
};
|
|
276
284
|
|
|
277
|
-
const getErrorResponse = (
|
|
285
|
+
const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
278
286
|
const prettyError = preparePrettyError(error);
|
|
279
287
|
logError(error, prettyError);
|
|
280
288
|
const errorProperty = getErrorProperty(error, prettyError);
|
|
281
|
-
return create$1(
|
|
289
|
+
return create$1(id, errorProperty);
|
|
282
290
|
};
|
|
283
291
|
|
|
284
292
|
const create = (message, result) => {
|
|
@@ -294,12 +302,28 @@ const getSuccessResponse = (message, result) => {
|
|
|
294
302
|
return create(message, resultProperty);
|
|
295
303
|
};
|
|
296
304
|
|
|
305
|
+
const getErrorResponseSimple = (id, error) => {
|
|
306
|
+
return {
|
|
307
|
+
jsonrpc: Two,
|
|
308
|
+
id,
|
|
309
|
+
error: {
|
|
310
|
+
code: Custom,
|
|
311
|
+
// @ts-ignore
|
|
312
|
+
message: error.message,
|
|
313
|
+
data: error
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
};
|
|
317
|
+
|
|
297
318
|
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
298
319
|
try {
|
|
299
320
|
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
300
321
|
return getSuccessResponse(message, result);
|
|
301
322
|
} catch (error) {
|
|
302
|
-
|
|
323
|
+
if (ipc.canUseSimpleErrorResponse) {
|
|
324
|
+
return getErrorResponseSimple(message.id, error);
|
|
325
|
+
}
|
|
326
|
+
return getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
303
327
|
}
|
|
304
328
|
};
|
|
305
329
|
|
|
@@ -356,7 +380,7 @@ const handleJsonRpcMessage = async (...args) => {
|
|
|
356
380
|
try {
|
|
357
381
|
ipc.send(response);
|
|
358
382
|
} catch (error) {
|
|
359
|
-
const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
|
|
383
|
+
const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
360
384
|
ipc.send(errorResponse);
|
|
361
385
|
}
|
|
362
386
|
return;
|