Skip to content

Commit baeb3e4

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore improved handling of errors that occur during the recursive execution and evaluation of JS files
1 parent 48c0f3c commit baeb3e4

File tree

33 files changed

+696
-105
lines changed

33 files changed

+696
-105
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#endif
1212

1313
using JavaScriptEngineSwitcher.Core;
14+
using JavaScriptEngineSwitcher.Core.Constants;
1415
using JavaScriptEngineSwitcher.Core.Extensions;
1516
using JavaScriptEngineSwitcher.Core.Utilities;
1617

@@ -341,18 +342,63 @@ private static WrapperException WrapJsException(OriginalException originalExcept
341342
if (errorValueType == JsValueType.Error
342343
|| errorValueType == JsValueType.Object)
343344
{
345+
JsPropertyId innerErrorPropertyId = JsPropertyId.FromString("innerException");
346+
if (errorValue.HasProperty(innerErrorPropertyId))
347+
{
348+
JsValue innerErrorValue = errorValue.GetProperty(innerErrorPropertyId);
349+
JsPropertyId metadataPropertyId = JsPropertyId.FromString("metadata");
350+
351+
if (innerErrorValue.HasProperty(metadataPropertyId))
352+
{
353+
errorValue = innerErrorValue;
354+
metadataValue = innerErrorValue.GetProperty(metadataPropertyId);
355+
}
356+
}
357+
344358
JsValue messagePropertyValue = errorValue.GetProperty("message");
345-
description = messagePropertyValue.ConvertToString().ToString();
359+
string localDescription = messagePropertyValue.ConvertToString().ToString();
360+
if (!string.IsNullOrWhiteSpace(localDescription))
361+
{
362+
description = localDescription;
363+
}
346364

347365
JsValue namePropertyValue = errorValue.GetProperty("name");
348366
type = namePropertyValue.ValueType == JsValueType.String ?
349-
namePropertyValue.ConvertToString().ToString() : string.Empty;
367+
namePropertyValue.ToString() : string.Empty;
368+
369+
JsPropertyId descriptionPropertyId = JsPropertyId.FromString("description");
370+
if (errorValue.HasProperty(descriptionPropertyId))
371+
{
372+
JsValue descriptionPropertyValue = errorValue.GetProperty(descriptionPropertyId);
373+
localDescription = descriptionPropertyValue.ConvertToString().ToString();
374+
if (!string.IsNullOrWhiteSpace(localDescription))
375+
{
376+
description = localDescription;
377+
}
378+
}
379+
380+
if (type == JsErrorType.Syntax)
381+
{
382+
errorCode = JsErrorCode.ScriptCompile;
383+
}
384+
else
385+
{
386+
JsPropertyId numberPropertyId = JsPropertyId.FromString("number");
387+
if (errorValue.HasProperty(numberPropertyId))
388+
{
389+
JsValue numberPropertyValue = errorValue.GetProperty(numberPropertyId);
390+
int errorNumber = numberPropertyValue.ValueType == JsValueType.Number ?
391+
numberPropertyValue.ToInt32() : 0;
392+
errorCode = (JsErrorCode)errorNumber;
393+
}
394+
}
350395

351396
JsPropertyId urlPropertyId = JsPropertyId.FromString("url");
352397
if (metadataValue.HasProperty(urlPropertyId))
353398
{
354399
JsValue urlPropertyValue = metadataValue.GetProperty(urlPropertyId);
355-
string url = urlPropertyValue.ConvertToString().ToString();
400+
string url = urlPropertyValue.ValueType == JsValueType.String ?
401+
urlPropertyValue.ToString() : string.Empty;
356402
if (url != "undefined")
357403
{
358404
documentName = url;
@@ -363,42 +409,34 @@ private static WrapperException WrapJsException(OriginalException originalExcept
363409
if (metadataValue.HasProperty(linePropertyId))
364410
{
365411
JsValue linePropertyValue = metadataValue.GetProperty(linePropertyId);
366-
lineNumber = linePropertyValue.ConvertToNumber().ToInt32() + 1;
412+
lineNumber = linePropertyValue.ValueType == JsValueType.Number ?
413+
linePropertyValue.ToInt32() + 1 : 0;
367414
}
368415

369416
JsPropertyId columnPropertyId = JsPropertyId.FromString("column");
370417
if (metadataValue.HasProperty(columnPropertyId))
371418
{
372419
JsValue columnPropertyValue = metadataValue.GetProperty(columnPropertyId);
373-
columnNumber = columnPropertyValue.ConvertToNumber().ToInt32() + 1;
420+
columnNumber = columnPropertyValue.ValueType == JsValueType.Number ?
421+
columnPropertyValue.ToInt32() + 1 : 0;
374422
}
375423

376424
string sourceLine = string.Empty;
377425
JsPropertyId sourcePropertyId = JsPropertyId.FromString("source");
378426
if (metadataValue.HasProperty(sourcePropertyId))
379427
{
380428
JsValue sourcePropertyValue = metadataValue.GetProperty(sourcePropertyId);
381-
sourceLine = sourcePropertyValue.ConvertToString().ToString();
429+
sourceLine = sourcePropertyValue.ValueType == JsValueType.String ?
430+
sourcePropertyValue.ToString() : string.Empty;
382431
sourceFragment = TextHelpers.GetTextFragmentFromLine(sourceLine, columnNumber);
383432
}
384433

385434
JsPropertyId stackPropertyId = JsPropertyId.FromString("stack");
386435
if (errorValue.HasProperty(stackPropertyId))
387436
{
388-
JsPropertyId descriptionPropertyId = JsPropertyId.FromString("description");
389-
if (errorValue.HasProperty(descriptionPropertyId))
390-
{
391-
JsValue descriptionPropertyValue = errorValue.GetProperty(descriptionPropertyId);
392-
if (descriptionPropertyValue.ValueType == JsValueType.String
393-
|| descriptionPropertyValue.StringLength > 0)
394-
{
395-
description = descriptionPropertyValue.ConvertToString().ToString();
396-
}
397-
}
398-
399437
JsValue stackPropertyValue = errorValue.GetProperty(stackPropertyId);
400438
string messageWithTypeAndCallStack = stackPropertyValue.ValueType == JsValueType.String ?
401-
stackPropertyValue.ConvertToString().ToString() : string.Empty;
439+
stackPropertyValue.ToString() : string.Empty;
402440
string messageWithType = errorValue.ConvertToString().ToString();
403441
string rawCallStack = messageWithTypeAndCallStack
404442
.TrimStart(messageWithType)
@@ -429,6 +467,11 @@ private static WrapperException WrapJsException(OriginalException originalExcept
429467
lineNumber, columnNumber, sourceFragment);
430468
}
431469
}
470+
else if (errorValueType == JsValueType.String)
471+
{
472+
message = errorValue.ToString();
473+
description = message;
474+
}
432475
else
433476
{
434477
message = errorValue.ConvertToString().ToString();

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/TypeMapper.cs

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
using JavaScriptEngineSwitcher.Core.Extensions;
1111
using JavaScriptEngineSwitcher.Core.Utilities;
1212

13+
using CoreErrorHelpers = JavaScriptEngineSwitcher.Core.Helpers.JsErrorHelpers;
1314
using WrapperException = JavaScriptEngineSwitcher.Core.JsException;
15+
using WrapperRuntimeException = JavaScriptEngineSwitcher.Core.JsRuntimeException;
16+
using WrapperScriptException = JavaScriptEngineSwitcher.Core.JsScriptException;
1417

1518
using JavaScriptEngineSwitcher.ChakraCore.Helpers;
1619
using JavaScriptEngineSwitcher.ChakraCore.JsRt.Embedding;
@@ -316,7 +319,7 @@ private EmbeddedObject CreateEmbeddedFunction(Delegate del)
316319
Exception exception = UnwrapException(e);
317320
var wrapperException = exception as WrapperException;
318321
JsValue errorValue = wrapperException != null ?
319-
JsValue.FromString(wrapperException.Message)
322+
CreateErrorFromWrapperException(wrapperException)
320323
:
321324
JsErrorHelpers.CreateError(string.Format(
322325
Strings.Runtime_HostDelegateInvocationFailed, exception.Message))
@@ -420,7 +423,7 @@ private EmbeddedType CreateEmbeddedType(Type type)
420423
Exception exception = UnwrapException(e);
421424
var wrapperException = exception as WrapperException;
422425
JsValue errorValue = wrapperException != null ?
423-
JsValue.FromString(wrapperException.Message)
426+
CreateErrorFromWrapperException(wrapperException)
424427
:
425428
JsErrorHelpers.CreateError(string.Format(
426429
Strings.Runtime_HostTypeConstructorInvocationFailed, typeName, exception.Message))
@@ -520,7 +523,7 @@ private void ProjectFields(EmbeddedItem externalItem)
520523

521524
if (wrapperException != null)
522525
{
523-
errorValue = JsValue.FromString(wrapperException.Message);
526+
errorValue = CreateErrorFromWrapperException(wrapperException);
524527
}
525528
else
526529
{
@@ -571,7 +574,7 @@ private void ProjectFields(EmbeddedItem externalItem)
571574

572575
if (wrapperException != null)
573576
{
574-
errorValue = JsValue.FromString(wrapperException.Message);
577+
errorValue = CreateErrorFromWrapperException(wrapperException);
575578
}
576579
else
577580
{
@@ -644,7 +647,7 @@ private void ProjectProperties(EmbeddedItem externalItem)
644647

645648
if (wrapperException != null)
646649
{
647-
errorValue = JsValue.FromString(wrapperException.Message);
650+
errorValue = CreateErrorFromWrapperException(wrapperException);
648651
}
649652
else
650653
{
@@ -698,7 +701,7 @@ private void ProjectProperties(EmbeddedItem externalItem)
698701

699702
if (wrapperException != null)
700703
{
701-
errorValue = JsValue.FromString(wrapperException.Message);
704+
errorValue = CreateErrorFromWrapperException(wrapperException);
702705
}
703706
else
704707
{
@@ -783,7 +786,7 @@ private void ProjectMethods(EmbeddedItem externalItem)
783786

784787
if (wrapperException != null)
785788
{
786-
errorValue = JsValue.FromString(wrapperException.Message);
789+
errorValue = CreateErrorFromWrapperException(wrapperException);
787790
}
788791
else
789792
{
@@ -881,6 +884,73 @@ private static Exception UnwrapException(Exception exception)
881884
return originalException;
882885
}
883886

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+
884954
#region IDisposable implementation
885955

886956
/// <summary>

0 commit comments

Comments
 (0)