1
1
// -------------------------------------------------------------------------------------------------------
2
2
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
3
+ // Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
3
4
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4
5
// -------------------------------------------------------------------------------------------------------
5
6
#include " stdafx.h"
@@ -1766,10 +1767,21 @@ JsValueRef __stdcall WScriptJsrt::GetProxyPropertiesCallback(JsValueRef callee,
1766
1767
bool WScriptJsrt::PrintException (LPCSTR fileName, JsErrorCode jsErrorCode, JsValueRef exception )
1767
1768
{
1768
1769
LPCWSTR errorTypeString = ConvertErrorCodeToMessage (jsErrorCode);
1770
+ JsValueRef metaData = JS_INVALID_REFERENCE;
1769
1771
1770
1772
if (exception == nullptr )
1771
1773
{
1772
- ChakraRTInterface::JsGetAndClearException (&exception );
1774
+ if (ChakraRTInterface::JsGetAndClearExceptionWithMetadata (&metaData) == JsNoError)
1775
+ {
1776
+ JsPropertyIdRef exceptionId = JS_INVALID_REFERENCE;
1777
+ IfJsrtErrorFail (CreatePropertyIdFromString (" exception" , &exceptionId), false );
1778
+ IfJsrtErrorFail (ChakraRTInterface::JsGetProperty (metaData, exceptionId, &exception ), false );
1779
+ }
1780
+ else
1781
+ {
1782
+ IfJsrtErrorFail (ChakraRTInterface::JsGetAndClearException (&exception ), false );
1783
+ }
1784
+
1773
1785
}
1774
1786
1775
1787
if (HostConfigFlags::flags.MuteHostErrorMsgIsEnabled )
@@ -1783,7 +1795,56 @@ bool WScriptJsrt::PrintException(LPCSTR fileName, JsErrorCode jsErrorCode, JsVal
1783
1795
{
1784
1796
AutoString errorMessage;
1785
1797
1786
- IfJsrtErrorFail (errorMessage.Initialize (exception ), false );
1798
+ if (errorMessage.Initialize (exception ) != JsNoError)
1799
+ {
1800
+ fwprintf (stderr, _u (" ERROR attempting to coerce error to string, using alternate handler\n " ));
1801
+ bool hasException = false ;
1802
+ ChakraRTInterface::JsHasException (&hasException);
1803
+ if (hasException)
1804
+ {
1805
+ JsValueRef throwAway = JS_INVALID_REFERENCE;
1806
+ ChakraRTInterface::JsGetAndClearException (&throwAway);
1807
+ }
1808
+ JsPropertyIdRef messagePropertyId = JS_INVALID_REFERENCE;
1809
+ IfJsrtErrorFail (CreatePropertyIdFromString (" message" , &messagePropertyId), false );
1810
+ JsValueRef message = JS_INVALID_REFERENCE;
1811
+ IfJsrtErrorFail (ChakraRTInterface::JsGetProperty (exception , messagePropertyId, &message), false );
1812
+ IfJsrtErrorFail (errorMessage.Initialize (message), false );
1813
+
1814
+ if (jsErrorCode != JsErrorCode::JsErrorScriptCompile)
1815
+ {
1816
+ CHAR shortFileName[_MAX_PATH];
1817
+ CHAR ext[_MAX_EXT];
1818
+ _splitpath_s (fileName, nullptr , 0 , nullptr , 0 , shortFileName, _countof (shortFileName), ext, _countof (ext));
1819
+
1820
+ if (metaData != JS_INVALID_REFERENCE)
1821
+ {
1822
+ JsPropertyIdRef linePropertyId = JS_INVALID_REFERENCE;
1823
+ JsValueRef lineProperty = JS_INVALID_REFERENCE;
1824
+
1825
+ JsPropertyIdRef columnPropertyId = JS_INVALID_REFERENCE;
1826
+ JsValueRef columnProperty = JS_INVALID_REFERENCE;
1827
+
1828
+ int line;
1829
+ int column;
1830
+
1831
+ IfJsrtErrorFail (CreatePropertyIdFromString (" line" , &linePropertyId), false );
1832
+ IfJsrtErrorFail (ChakraRTInterface::JsGetProperty (metaData, linePropertyId, &lineProperty), false );
1833
+ IfJsrtErrorFail (ChakraRTInterface::JsNumberToInt (lineProperty, &line), false );
1834
+
1835
+ IfJsrtErrorFail (CreatePropertyIdFromString (" column" , &columnPropertyId), false );
1836
+ IfJsrtErrorFail (ChakraRTInterface::JsGetProperty (metaData, columnPropertyId, &columnProperty), false );
1837
+ IfJsrtErrorFail (ChakraRTInterface::JsNumberToInt (columnProperty, &column), false );
1838
+ fwprintf (stderr, _u (" %ls\n at code (%S%S:%d:%d)\n " ),
1839
+ errorMessage.GetWideString (), shortFileName, ext, line + 1 , column + 1 );
1840
+ }
1841
+ else
1842
+ {
1843
+ fwprintf (stderr, _u (" %ls\n\t at code (%S%S:\?\? :\?\? )\n " ), errorMessage.GetWideString (), shortFileName, ext);
1844
+ }
1845
+ return true ;
1846
+ }
1847
+ }
1787
1848
1788
1849
if (jsErrorCode == JsErrorCode::JsErrorScriptCompile)
1789
1850
{
0 commit comments