4
4
using System . Linq ;
5
5
using System . Reflection ;
6
6
using System . Runtime . InteropServices ;
7
+ using System . Text ;
7
8
8
9
using JavaScriptEngineSwitcher . Core ;
9
10
using JavaScriptEngineSwitcher . Core . Utilities ;
@@ -844,6 +845,7 @@ private static HostException ConvertScriptExceptionToHostException(ScriptExcepti
844
845
}
845
846
else
846
847
{
848
+ string documentName = string . Empty ;
847
849
int lineNumber = 0 ;
848
850
int columnNumber = 0 ;
849
851
string sourceFragment = string . Empty ;
@@ -858,20 +860,11 @@ private static HostException ConvertScriptExceptionToHostException(ScriptExcepti
858
860
{
859
861
JsValue errorValue = metadataValue . GetProperty ( "exception" ) ;
860
862
861
- JsPropertyId stackPropertyId = JsPropertyId . FromString ( "stack " ) ;
862
- if ( errorValue . HasProperty ( stackPropertyId ) )
863
+ JsPropertyId urlPropertyId = JsPropertyId . FromString ( "url " ) ;
864
+ if ( metadataValue . HasProperty ( urlPropertyId ) )
863
865
{
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 ( ) ;
875
868
}
876
869
877
870
JsPropertyId linePropertyId = JsPropertyId . FromString ( "line" ) ;
@@ -894,6 +887,20 @@ private static HostException ConvertScriptExceptionToHostException(ScriptExcepti
894
887
JsValue sourcePropertyValue = metadataValue . GetProperty ( sourcePropertyId ) ;
895
888
sourceFragment = sourcePropertyValue . ConvertToString ( ) . ToString ( ) ;
896
889
}
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
+ }
897
904
}
898
905
}
899
906
else if ( scriptException is JsUsageException )
@@ -923,6 +930,44 @@ private static HostException ConvertScriptExceptionToHostException(ScriptExcepti
923
930
return hostException ;
924
931
}
925
932
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
+
926
971
#endregion
927
972
928
973
#region JsEngineBase overrides
0 commit comments