@lvce-editor/chat-tool-worker 9.14.0 → 9.16.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.
@@ -541,7 +541,10 @@ const constructError = (message, type, name) => {
541
541
  if (ErrorConstructor === Error) {
542
542
  const error = new Error(message);
543
543
  if (name && name !== 'VError') {
544
- error.name = name;
544
+ Object.defineProperty(error, 'name', {
545
+ configurable: true,
546
+ value: name
547
+ });
545
548
  }
546
549
  return error;
547
550
  }
@@ -558,8 +561,10 @@ const getCurrentStack = () => {
558
561
  const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
559
562
  return currentStack;
560
563
  };
561
- const getNewLineIndex = (string, startIndex = undefined) => {
562
- return string.indexOf(NewLine, startIndex);
564
+ const getNewLineIndex = (string, startIndex) => {
565
+ {
566
+ return string.indexOf(NewLine);
567
+ }
563
568
  };
564
569
  const getParentStack = error => {
565
570
  let parentStack = error.stack || error.data || error.message || '';
@@ -570,55 +575,91 @@ const getParentStack = error => {
570
575
  };
571
576
  const MethodNotFound = -32601;
572
577
  const Custom = -32001;
578
+ const setStack = (error, stack) => {
579
+ const descriptor = Object.getOwnPropertyDescriptor(error, 'stack');
580
+ if (descriptor) {
581
+ if (!descriptor.configurable && !descriptor.writable) {
582
+ return;
583
+ }
584
+ if (!descriptor.configurable && descriptor.writable) {
585
+ error.stack = stack;
586
+ return;
587
+ }
588
+ }
589
+ Object.defineProperty(error, 'stack', {
590
+ configurable: true,
591
+ value: stack,
592
+ writable: true
593
+ });
594
+ };
595
+ const restoreExistingError = (error, currentStack) => {
596
+ if (typeof error.stack === 'string') {
597
+ setStack(error, `${error.stack}${NewLine}${currentStack}`);
598
+ }
599
+ return error;
600
+ };
601
+ const restoreMethodNotFoundError = (error, currentStack) => {
602
+ const restoredError = new JsonRpcError(error.message);
603
+ const parentStack = getParentStack(error);
604
+ setStack(restoredError, `${parentStack}${NewLine}${currentStack}`);
605
+ return restoredError;
606
+ };
607
+ const restoreStackFromData = (restoredError, error, currentStack) => {
608
+ if (error.data.stack && error.data.type && error.message) {
609
+ setStack(restoredError, `${error.data.type}: ${error.message}${NewLine}${error.data.stack}${NewLine}${currentStack}`);
610
+ return;
611
+ }
612
+ if (error.data.stack) {
613
+ setStack(restoredError, error.data.stack);
614
+ }
615
+ };
616
+ const applyDataProperties = (restoredError, error) => {
617
+ restoreStackFromData(restoredError, error, getCurrentStack());
618
+ if (error.data.codeFrame) {
619
+ // @ts-ignore
620
+ restoredError.codeFrame = error.data.codeFrame;
621
+ }
622
+ if (error.data.code) {
623
+ // @ts-ignore
624
+ restoredError.code = error.data.code;
625
+ }
626
+ if (error.data.type) {
627
+ // @ts-ignore
628
+ restoredError.name = error.data.type;
629
+ }
630
+ };
631
+ const applyDirectProperties = (restoredError, error) => {
632
+ if (error.stack) {
633
+ const lowerStack = restoredError.stack || '';
634
+ const indexNewLine = getNewLineIndex(lowerStack);
635
+ const parentStack = getParentStack(error);
636
+ // @ts-ignore
637
+ setStack(restoredError, `${parentStack}${lowerStack.slice(indexNewLine)}`);
638
+ }
639
+ if (error.codeFrame) {
640
+ // @ts-ignore
641
+ restoredError.codeFrame = error.codeFrame;
642
+ }
643
+ };
644
+ const restoreMessageError = (error, _currentStack) => {
645
+ const restoredError = constructError(error.message, error.type, error.name);
646
+ if (error.data) {
647
+ applyDataProperties(restoredError, error);
648
+ } else {
649
+ applyDirectProperties(restoredError, error);
650
+ }
651
+ return restoredError;
652
+ };
573
653
  const restoreJsonRpcError = error => {
574
654
  const currentStack = getCurrentStack();
575
655
  if (error && error instanceof Error) {
576
- if (typeof error.stack === 'string') {
577
- error.stack = error.stack + NewLine + currentStack;
578
- }
579
- return error;
656
+ return restoreExistingError(error, currentStack);
580
657
  }
581
658
  if (error && error.code && error.code === MethodNotFound) {
582
- const restoredError = new JsonRpcError(error.message);
583
- const parentStack = getParentStack(error);
584
- restoredError.stack = parentStack + NewLine + currentStack;
585
- return restoredError;
659
+ return restoreMethodNotFoundError(error, currentStack);
586
660
  }
587
661
  if (error && error.message) {
588
- const restoredError = constructError(error.message, error.type, error.name);
589
- if (error.data) {
590
- if (error.data.stack && error.data.type && error.message) {
591
- restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
592
- } else if (error.data.stack) {
593
- restoredError.stack = error.data.stack;
594
- }
595
- if (error.data.codeFrame) {
596
- // @ts-ignore
597
- restoredError.codeFrame = error.data.codeFrame;
598
- }
599
- if (error.data.code) {
600
- // @ts-ignore
601
- restoredError.code = error.data.code;
602
- }
603
- if (error.data.type) {
604
- // @ts-ignore
605
- restoredError.name = error.data.type;
606
- }
607
- } else {
608
- if (error.stack) {
609
- const lowerStack = restoredError.stack || '';
610
- // @ts-ignore
611
- const indexNewLine = getNewLineIndex(lowerStack);
612
- const parentStack = getParentStack(error);
613
- // @ts-ignore
614
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
615
- }
616
- if (error.codeFrame) {
617
- // @ts-ignore
618
- restoredError.codeFrame = error.codeFrame;
619
- }
620
- }
621
- return restoredError;
662
+ return restoreMessageError(error);
622
663
  }
623
664
  if (typeof error === 'string') {
624
665
  return new Error(`JsonRpc Error: ${error}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-tool-worker",
3
- "version": "9.14.0",
3
+ "version": "9.16.0",
4
4
  "description": "Chat Tool Worker",
5
5
  "repository": {
6
6
  "type": "git",