@lvce-editor/file-watcher-process 4.8.0 → 4.9.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 +86 -45
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -646,7 +646,10 @@ const constructError = (message, type, name) => {
646
646
  if (ErrorConstructor === Error) {
647
647
  const error = new Error(message);
648
648
  if (name && name !== 'VError') {
649
- error.name = name;
649
+ Object.defineProperty(error, 'name', {
650
+ configurable: true,
651
+ value: name
652
+ });
650
653
  }
651
654
  return error;
652
655
  }
@@ -663,8 +666,10 @@ const getCurrentStack = () => {
663
666
  const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
664
667
  return currentStack;
665
668
  };
666
- const getNewLineIndex = (string, startIndex = undefined) => {
667
- return string.indexOf(NewLine, startIndex);
669
+ const getNewLineIndex = (string, startIndex) => {
670
+ {
671
+ return string.indexOf(NewLine);
672
+ }
668
673
  };
669
674
  const getParentStack = error => {
670
675
  let parentStack = error.stack || error.data || error.message || '';
@@ -675,55 +680,91 @@ const getParentStack = error => {
675
680
  };
676
681
  const MethodNotFound = -32601;
677
682
  const Custom = -32001;
683
+ const setStack = (error, stack) => {
684
+ const descriptor = Object.getOwnPropertyDescriptor(error, 'stack');
685
+ if (descriptor) {
686
+ if (!descriptor.configurable && !descriptor.writable) {
687
+ return;
688
+ }
689
+ if (!descriptor.configurable && descriptor.writable) {
690
+ error.stack = stack;
691
+ return;
692
+ }
693
+ }
694
+ Object.defineProperty(error, 'stack', {
695
+ configurable: true,
696
+ value: stack,
697
+ writable: true
698
+ });
699
+ };
700
+ const restoreExistingError = (error, currentStack) => {
701
+ if (typeof error.stack === 'string') {
702
+ setStack(error, `${error.stack}${NewLine}${currentStack}`);
703
+ }
704
+ return error;
705
+ };
706
+ const restoreMethodNotFoundError = (error, currentStack) => {
707
+ const restoredError = new JsonRpcError(error.message);
708
+ const parentStack = getParentStack(error);
709
+ setStack(restoredError, `${parentStack}${NewLine}${currentStack}`);
710
+ return restoredError;
711
+ };
712
+ const restoreStackFromData = (restoredError, error, currentStack) => {
713
+ if (error.data.stack && error.data.type && error.message) {
714
+ setStack(restoredError, `${error.data.type}: ${error.message}${NewLine}${error.data.stack}${NewLine}${currentStack}`);
715
+ return;
716
+ }
717
+ if (error.data.stack) {
718
+ setStack(restoredError, error.data.stack);
719
+ }
720
+ };
721
+ const applyDataProperties = (restoredError, error) => {
722
+ restoreStackFromData(restoredError, error, getCurrentStack());
723
+ if (error.data.codeFrame) {
724
+ // @ts-ignore
725
+ restoredError.codeFrame = error.data.codeFrame;
726
+ }
727
+ if (error.data.code) {
728
+ // @ts-ignore
729
+ restoredError.code = error.data.code;
730
+ }
731
+ if (error.data.type) {
732
+ // @ts-ignore
733
+ restoredError.name = error.data.type;
734
+ }
735
+ };
736
+ const applyDirectProperties = (restoredError, error) => {
737
+ if (error.stack) {
738
+ const lowerStack = restoredError.stack || '';
739
+ const indexNewLine = getNewLineIndex(lowerStack);
740
+ const parentStack = getParentStack(error);
741
+ // @ts-ignore
742
+ setStack(restoredError, `${parentStack}${lowerStack.slice(indexNewLine)}`);
743
+ }
744
+ if (error.codeFrame) {
745
+ // @ts-ignore
746
+ restoredError.codeFrame = error.codeFrame;
747
+ }
748
+ };
749
+ const restoreMessageError = (error, _currentStack) => {
750
+ const restoredError = constructError(error.message, error.type, error.name);
751
+ if (error.data) {
752
+ applyDataProperties(restoredError, error);
753
+ } else {
754
+ applyDirectProperties(restoredError, error);
755
+ }
756
+ return restoredError;
757
+ };
678
758
  const restoreJsonRpcError = error => {
679
759
  const currentStack = getCurrentStack();
680
760
  if (error && error instanceof Error) {
681
- if (typeof error.stack === 'string') {
682
- error.stack = error.stack + NewLine + currentStack;
683
- }
684
- return error;
761
+ return restoreExistingError(error, currentStack);
685
762
  }
686
763
  if (error && error.code && error.code === MethodNotFound) {
687
- const restoredError = new JsonRpcError(error.message);
688
- const parentStack = getParentStack(error);
689
- restoredError.stack = parentStack + NewLine + currentStack;
690
- return restoredError;
764
+ return restoreMethodNotFoundError(error, currentStack);
691
765
  }
692
766
  if (error && error.message) {
693
- const restoredError = constructError(error.message, error.type, error.name);
694
- if (error.data) {
695
- if (error.data.stack && error.data.type && error.message) {
696
- restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
697
- } else if (error.data.stack) {
698
- restoredError.stack = error.data.stack;
699
- }
700
- if (error.data.codeFrame) {
701
- // @ts-ignore
702
- restoredError.codeFrame = error.data.codeFrame;
703
- }
704
- if (error.data.code) {
705
- // @ts-ignore
706
- restoredError.code = error.data.code;
707
- }
708
- if (error.data.type) {
709
- // @ts-ignore
710
- restoredError.name = error.data.type;
711
- }
712
- } else {
713
- if (error.stack) {
714
- const lowerStack = restoredError.stack || '';
715
- // @ts-ignore
716
- const indexNewLine = getNewLineIndex(lowerStack);
717
- const parentStack = getParentStack(error);
718
- // @ts-ignore
719
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
720
- }
721
- if (error.codeFrame) {
722
- // @ts-ignore
723
- restoredError.codeFrame = error.codeFrame;
724
- }
725
- }
726
- return restoredError;
767
+ return restoreMessageError(error);
727
768
  }
728
769
  if (typeof error === 'string') {
729
770
  return new Error(`JsonRpc Error: ${error}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-watcher-process",
3
- "version": "4.8.0",
3
+ "version": "4.9.0",
4
4
  "description": "File Watcher Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",