Skip to content

Commit 83eb59d

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore the compilation error messages now contains a information about the error location
1 parent d43fa66 commit 83eb59d

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

NuGet/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2020
* JavaScriptEngineSwitcher.ChakraCore.Native.debian-x64
2121
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</description>
2222
<summary>JavaScriptEngineSwitcher.ChakraCore contains adapter `ChakraCoreJsEngine` (wrapper for the ChakraCore).</summary>
23-
<releaseNotes>ChakraCore was updated to version 1.7.0.</releaseNotes>
23+
<releaseNotes>Compilation error messages now contains a information about the error location.</releaseNotes>
2424
<copyright>Copyright (c) 2013-2017 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2525
<language>en-US</language>
2626
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript ChakraCore</tags>

NuGet/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
ChakraCore was updated to version 1.7.0.
33+
Compilation error messages now contains a information about the error location.
3434

3535
=============
3636
DOCUMENTATION

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Runtime.InteropServices;
7+
using System.Text;
78

89
using JavaScriptEngineSwitcher.Core;
910
using JavaScriptEngineSwitcher.Core.Utilities;
@@ -844,6 +845,7 @@ private static HostException ConvertScriptExceptionToHostException(ScriptExcepti
844845
}
845846
else
846847
{
848+
string documentName = string.Empty;
847849
int lineNumber = 0;
848850
int columnNumber = 0;
849851
string sourceFragment = string.Empty;
@@ -858,20 +860,11 @@ private static HostException ConvertScriptExceptionToHostException(ScriptExcepti
858860
{
859861
JsValue errorValue = metadataValue.GetProperty("exception");
860862

861-
JsPropertyId stackPropertyId = JsPropertyId.FromString("stack");
862-
if (errorValue.HasProperty(stackPropertyId))
863+
JsPropertyId urlPropertyId = JsPropertyId.FromString("url");
864+
if (metadataValue.HasProperty(urlPropertyId))
863865
{
864-
JsValue stackPropertyValue = errorValue.GetProperty(stackPropertyId);
865-
message = stackPropertyValue.ConvertToString().ToString();
866-
}
867-
else
868-
{
869-
JsValue messagePropertyValue = errorValue.GetProperty("message");
870-
string scriptMessage = messagePropertyValue.ConvertToString().ToString();
871-
if (!string.IsNullOrWhiteSpace(scriptMessage))
872-
{
873-
message = string.Format("{0}: {1}", message.TrimEnd('.'), scriptMessage);
874-
}
866+
JsValue urlPropertyValue = metadataValue.GetProperty(urlPropertyId);
867+
documentName = urlPropertyValue.ConvertToString().ToString();
875868
}
876869

877870
JsPropertyId linePropertyId = JsPropertyId.FromString("line");
@@ -894,6 +887,20 @@ private static HostException ConvertScriptExceptionToHostException(ScriptExcepti
894887
JsValue sourcePropertyValue = metadataValue.GetProperty(sourcePropertyId);
895888
sourceFragment = sourcePropertyValue.ConvertToString().ToString();
896889
}
890+
891+
JsPropertyId stackPropertyId = JsPropertyId.FromString("stack");
892+
if (errorValue.HasProperty(stackPropertyId))
893+
{
894+
JsValue stackPropertyValue = errorValue.GetProperty(stackPropertyId);
895+
message = stackPropertyValue.ConvertToString().ToString();
896+
}
897+
else
898+
{
899+
JsValue messagePropertyValue = errorValue.GetProperty("message");
900+
string scriptMessage = messagePropertyValue.ConvertToString().ToString();
901+
message = GenerateErrorMessageWithLocation(message.TrimEnd('.'), scriptMessage,
902+
documentName, lineNumber, columnNumber);
903+
}
897904
}
898905
}
899906
else if (scriptException is JsUsageException)
@@ -923,6 +930,44 @@ private static HostException ConvertScriptExceptionToHostException(ScriptExcepti
923930
return hostException;
924931
}
925932

933+
/// <summary>
934+
/// Generates a error message with location
935+
/// </summary>
936+
/// <param name="category">Error category</param>
937+
/// <param name="message">Error message</param>
938+
/// <param name="documentName">Document name</param>
939+
/// <param name="lineNumber">Line number</param>
940+
/// <param name="columnNumber">Column number</param>
941+
/// <returns>Error message with location</returns>
942+
private static string GenerateErrorMessageWithLocation(string category, string message,
943+
string documentName, int lineNumber, int columnNumber)
944+
{
945+
var messageBuilder = new StringBuilder();
946+
if (!string.IsNullOrWhiteSpace(category))
947+
{
948+
messageBuilder.AppendFormat("{0}: ", category);
949+
}
950+
messageBuilder.Append(message);
951+
if (!string.IsNullOrWhiteSpace(documentName))
952+
{
953+
messageBuilder.AppendLine();
954+
messageBuilder.AppendFormat(" at {0}", documentName);
955+
if (lineNumber > 0)
956+
{
957+
messageBuilder.AppendFormat(":{0}", lineNumber);
958+
if (columnNumber > 0)
959+
{
960+
messageBuilder.AppendFormat(":{0}", columnNumber);
961+
}
962+
}
963+
}
964+
965+
string errorMessage = messageBuilder.ToString();
966+
messageBuilder.Clear();
967+
968+
return errorMessage;
969+
}
970+
926971
#endregion
927972

928973
#region JsEngineBase overrides

0 commit comments

Comments
 (0)