@lvce-editor/json-rpc 8.0.0 → 8.1.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.
Files changed (2) hide show
  1. package/dist/index.js +68 -44
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -134,8 +134,10 @@ const getCurrentStack = () => {
134
134
  return currentStack;
135
135
  };
136
136
 
137
- const getNewLineIndex = (string, startIndex = undefined) => {
138
- return string.indexOf(NewLine, startIndex);
137
+ const getNewLineIndex = (string, startIndex) => {
138
+ {
139
+ return string.indexOf(NewLine);
140
+ }
139
141
  };
140
142
 
141
143
  const getParentStack = error => {
@@ -149,55 +151,77 @@ const getParentStack = error => {
149
151
  const MethodNotFound = -32601;
150
152
  const Custom = -32001;
151
153
 
154
+ const restoreExistingError = (error, currentStack) => {
155
+ if (typeof error.stack === 'string') {
156
+ error.stack = error.stack + NewLine + currentStack;
157
+ }
158
+ return error;
159
+ };
160
+ const restoreMethodNotFoundError = (error, currentStack) => {
161
+ const restoredError = new JsonRpcError(error.message);
162
+ const parentStack = getParentStack(error);
163
+ restoredError.stack = parentStack + NewLine + currentStack;
164
+ return restoredError;
165
+ };
166
+ const restoreStackFromData = (restoredError, error, currentStack) => {
167
+ if (error.data.stack && error.data.type && error.message) {
168
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
169
+ return;
170
+ }
171
+ if (error.data.stack) {
172
+ restoredError.stack = error.data.stack;
173
+ }
174
+ };
175
+ const applyDataProperties = (restoredError, error) => {
176
+ if (!error.data) {
177
+ return;
178
+ }
179
+ restoreStackFromData(restoredError, error, getCurrentStack());
180
+ if (error.data.codeFrame) {
181
+ // @ts-ignore
182
+ restoredError.codeFrame = error.data.codeFrame;
183
+ }
184
+ if (error.data.code) {
185
+ // @ts-ignore
186
+ restoredError.code = error.data.code;
187
+ }
188
+ if (error.data.type) {
189
+ // @ts-ignore
190
+ restoredError.name = error.data.type;
191
+ }
192
+ };
193
+ const applyDirectProperties = (restoredError, error) => {
194
+ if (error.stack) {
195
+ const lowerStack = restoredError.stack || '';
196
+ const indexNewLine = getNewLineIndex(lowerStack);
197
+ const parentStack = getParentStack(error);
198
+ // @ts-ignore
199
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
200
+ }
201
+ if (error.codeFrame) {
202
+ // @ts-ignore
203
+ restoredError.codeFrame = error.codeFrame;
204
+ }
205
+ };
206
+ const restoreMessageError = (error, _currentStack) => {
207
+ const restoredError = constructError(error.message, error.type, error.name);
208
+ if (error.data) {
209
+ applyDataProperties(restoredError, error);
210
+ } else {
211
+ applyDirectProperties(restoredError, error);
212
+ }
213
+ return restoredError;
214
+ };
152
215
  const restoreJsonRpcError = error => {
153
216
  const currentStack = getCurrentStack();
154
217
  if (error && error instanceof Error) {
155
- if (typeof error.stack === 'string') {
156
- error.stack = error.stack + NewLine + currentStack;
157
- }
158
- return error;
218
+ return restoreExistingError(error, currentStack);
159
219
  }
160
220
  if (error && error.code && error.code === MethodNotFound) {
161
- const restoredError = new JsonRpcError(error.message);
162
- const parentStack = getParentStack(error);
163
- restoredError.stack = parentStack + NewLine + currentStack;
164
- return restoredError;
221
+ return restoreMethodNotFoundError(error, currentStack);
165
222
  }
166
223
  if (error && error.message) {
167
- const restoredError = constructError(error.message, error.type, error.name);
168
- if (error.data) {
169
- if (error.data.stack && error.data.type && error.message) {
170
- restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
171
- } else if (error.data.stack) {
172
- restoredError.stack = error.data.stack;
173
- }
174
- if (error.data.codeFrame) {
175
- // @ts-ignore
176
- restoredError.codeFrame = error.data.codeFrame;
177
- }
178
- if (error.data.code) {
179
- // @ts-ignore
180
- restoredError.code = error.data.code;
181
- }
182
- if (error.data.type) {
183
- // @ts-ignore
184
- restoredError.name = error.data.type;
185
- }
186
- } else {
187
- if (error.stack) {
188
- const lowerStack = restoredError.stack || '';
189
- // @ts-ignore
190
- const indexNewLine = getNewLineIndex(lowerStack);
191
- const parentStack = getParentStack(error);
192
- // @ts-ignore
193
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
194
- }
195
- if (error.codeFrame) {
196
- // @ts-ignore
197
- restoredError.codeFrame = error.codeFrame;
198
- }
199
- }
200
- return restoredError;
224
+ return restoreMessageError(error);
201
225
  }
202
226
  if (typeof error === 'string') {
203
227
  return new Error(`JsonRpc Error: ${error}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/json-rpc",
3
- "version": "8.0.0",
3
+ "version": "8.1.0",
4
4
  "description": "JsonRpc implementation",
5
5
  "repository": {
6
6
  "type": "git",