|
10 | 10 | using JavaScriptEngineSwitcher.Core.Extensions;
|
11 | 11 | using JavaScriptEngineSwitcher.Core.Utilities;
|
12 | 12 |
|
| 13 | +using CoreErrorHelpers = JavaScriptEngineSwitcher.Core.Helpers.JsErrorHelpers; |
13 | 14 | using WrapperException = JavaScriptEngineSwitcher.Core.JsException;
|
| 15 | +using WrapperRuntimeException = JavaScriptEngineSwitcher.Core.JsRuntimeException; |
| 16 | +using WrapperScriptException = JavaScriptEngineSwitcher.Core.JsScriptException; |
14 | 17 |
|
15 | 18 | using JavaScriptEngineSwitcher.ChakraCore.Helpers;
|
16 | 19 | using JavaScriptEngineSwitcher.ChakraCore.JsRt.Embedding;
|
@@ -316,7 +319,7 @@ private EmbeddedObject CreateEmbeddedFunction(Delegate del)
|
316 | 319 | Exception exception = UnwrapException(e);
|
317 | 320 | var wrapperException = exception as WrapperException;
|
318 | 321 | JsValue errorValue = wrapperException != null ?
|
319 |
| - JsValue.FromString(wrapperException.Message) |
| 322 | + CreateErrorFromWrapperException(wrapperException) |
320 | 323 | :
|
321 | 324 | JsErrorHelpers.CreateError(string.Format(
|
322 | 325 | Strings.Runtime_HostDelegateInvocationFailed, exception.Message))
|
@@ -420,7 +423,7 @@ private EmbeddedType CreateEmbeddedType(Type type)
|
420 | 423 | Exception exception = UnwrapException(e);
|
421 | 424 | var wrapperException = exception as WrapperException;
|
422 | 425 | JsValue errorValue = wrapperException != null ?
|
423 |
| - JsValue.FromString(wrapperException.Message) |
| 426 | + CreateErrorFromWrapperException(wrapperException) |
424 | 427 | :
|
425 | 428 | JsErrorHelpers.CreateError(string.Format(
|
426 | 429 | Strings.Runtime_HostTypeConstructorInvocationFailed, typeName, exception.Message))
|
@@ -520,7 +523,7 @@ private void ProjectFields(EmbeddedItem externalItem)
|
520 | 523 |
|
521 | 524 | if (wrapperException != null)
|
522 | 525 | {
|
523 |
| - errorValue = JsValue.FromString(wrapperException.Message); |
| 526 | + errorValue = CreateErrorFromWrapperException(wrapperException); |
524 | 527 | }
|
525 | 528 | else
|
526 | 529 | {
|
@@ -571,7 +574,7 @@ private void ProjectFields(EmbeddedItem externalItem)
|
571 | 574 |
|
572 | 575 | if (wrapperException != null)
|
573 | 576 | {
|
574 |
| - errorValue = JsValue.FromString(wrapperException.Message); |
| 577 | + errorValue = CreateErrorFromWrapperException(wrapperException); |
575 | 578 | }
|
576 | 579 | else
|
577 | 580 | {
|
@@ -644,7 +647,7 @@ private void ProjectProperties(EmbeddedItem externalItem)
|
644 | 647 |
|
645 | 648 | if (wrapperException != null)
|
646 | 649 | {
|
647 |
| - errorValue = JsValue.FromString(wrapperException.Message); |
| 650 | + errorValue = CreateErrorFromWrapperException(wrapperException); |
648 | 651 | }
|
649 | 652 | else
|
650 | 653 | {
|
@@ -698,7 +701,7 @@ private void ProjectProperties(EmbeddedItem externalItem)
|
698 | 701 |
|
699 | 702 | if (wrapperException != null)
|
700 | 703 | {
|
701 |
| - errorValue = JsValue.FromString(wrapperException.Message); |
| 704 | + errorValue = CreateErrorFromWrapperException(wrapperException); |
702 | 705 | }
|
703 | 706 | else
|
704 | 707 | {
|
@@ -783,7 +786,7 @@ private void ProjectMethods(EmbeddedItem externalItem)
|
783 | 786 |
|
784 | 787 | if (wrapperException != null)
|
785 | 788 | {
|
786 |
| - errorValue = JsValue.FromString(wrapperException.Message); |
| 789 | + errorValue = CreateErrorFromWrapperException(wrapperException); |
787 | 790 | }
|
788 | 791 | else
|
789 | 792 | {
|
@@ -881,6 +884,73 @@ private static Exception UnwrapException(Exception exception)
|
881 | 884 | return originalException;
|
882 | 885 | }
|
883 | 886 |
|
| 887 | + private static JsValue CreateErrorFromWrapperException(WrapperException exception) |
| 888 | + { |
| 889 | + var originalException = exception.InnerException as JsException; |
| 890 | + JsErrorCode errorCode = originalException != null ? |
| 891 | + originalException.ErrorCode : JsErrorCode.NoError; |
| 892 | + string description = exception.Description; |
| 893 | + |
| 894 | + JsValue innerErrorValue = JsErrorHelpers.CreateError(description); |
| 895 | + innerErrorValue.SetProperty("description", JsValue.FromString(description), true); |
| 896 | + |
| 897 | + JsValue metadataValue = JsValue.CreateObject(); |
| 898 | + |
| 899 | + var scriptException = exception as WrapperScriptException; |
| 900 | + if (scriptException != null) |
| 901 | + { |
| 902 | + string type = scriptException.Type; |
| 903 | + string documentName = scriptException.DocumentName; |
| 904 | + int lineNumber = scriptException.LineNumber; |
| 905 | + if (lineNumber > 0) |
| 906 | + { |
| 907 | + lineNumber--; |
| 908 | + } |
| 909 | + int columnNumber = scriptException.ColumnNumber; |
| 910 | + if (columnNumber > 0) |
| 911 | + { |
| 912 | + columnNumber--; |
| 913 | + } |
| 914 | + string sourceFragment = scriptException.SourceFragment; |
| 915 | + |
| 916 | + innerErrorValue.SetProperty("name", JsValue.FromString(type), true); |
| 917 | + |
| 918 | + var runtimeException = scriptException as WrapperRuntimeException; |
| 919 | + if (runtimeException != null) |
| 920 | + { |
| 921 | + var errorNumber = (int)errorCode; |
| 922 | + string callStack = runtimeException.CallStack; |
| 923 | + string messageWithTypeAndCallStack = CoreErrorHelpers.GenerateScriptErrorMessage(type, |
| 924 | + description, callStack); |
| 925 | + |
| 926 | + innerErrorValue.SetProperty("number", JsValue.FromInt32(errorNumber), true); |
| 927 | + if (!string.IsNullOrWhiteSpace(callStack)) |
| 928 | + { |
| 929 | + innerErrorValue.SetProperty("stack", JsValue.FromString(messageWithTypeAndCallStack), true); |
| 930 | + } |
| 931 | + } |
| 932 | + else |
| 933 | + { |
| 934 | + innerErrorValue.SetProperty("url", JsValue.FromString(documentName), true); |
| 935 | + innerErrorValue.SetProperty("line", JsValue.FromInt32(lineNumber), true); |
| 936 | + innerErrorValue.SetProperty("column", JsValue.FromInt32(columnNumber), true); |
| 937 | + innerErrorValue.SetProperty("source", JsValue.FromString(sourceFragment), true); |
| 938 | + } |
| 939 | + |
| 940 | + metadataValue.SetProperty("url", JsValue.FromString(documentName), true); |
| 941 | + metadataValue.SetProperty("line", JsValue.FromInt32(lineNumber), true); |
| 942 | + metadataValue.SetProperty("column", JsValue.FromInt32(columnNumber), true); |
| 943 | + metadataValue.SetProperty("source", JsValue.FromString(sourceFragment), true); |
| 944 | + } |
| 945 | + |
| 946 | + innerErrorValue.SetProperty("metadata", metadataValue, true); |
| 947 | + |
| 948 | + JsValue errorValue = JsErrorHelpers.CreateError(description); |
| 949 | + errorValue.SetProperty("innerException", innerErrorValue, true); |
| 950 | + |
| 951 | + return errorValue; |
| 952 | + } |
| 953 | + |
884 | 954 | #region IDisposable implementation
|
885 | 955 |
|
886 | 956 | /// <summary>
|
|
0 commit comments