44using System . Linq ;
55using System . Reflection ;
66using System . Runtime . InteropServices ;
7+ using System . Text ;
78
89using JavaScriptEngineSwitcher . Core ;
910using 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