in ClearScript/Windows/Core/WindowsScriptItem.cs [56:125]
private bool TryGetScriptError(Exception exception, out IScriptEngineException scriptError)
{
// WORKAROUND: Windows Script items often throw ugly exceptions. The code here
// attempts to clean up specific cases.
while (exception is TargetInvocationException)
{
exception = exception.InnerException;
}
scriptError = exception as IScriptEngineException;
if (scriptError != null)
{
return true;
}
if (exception is COMException comException)
{
var result = comException.ErrorCode;
if (((result == HResult.SCRIPT_E_REPORTED) || (result == HResult.CLEARSCRIPT_E_HOSTEXCEPTION)) && (engine.CurrentScriptFrame != null))
{
scriptError = engine.CurrentScriptFrame.ScriptError ?? engine.CurrentScriptFrame.PendingScriptError;
if (scriptError != null)
{
return true;
}
var hostException = engine.CurrentScriptFrame.HostException;
if (hostException != null)
{
scriptError = new ScriptEngineException(engine.Name, hostException.Message, null, HResult.CLEARSCRIPT_E_HOSTEXCEPTION, false, true, null, hostException);
return true;
}
}
else if (HResult.GetFacility(result) == HResult.FACILITY_CONTROL)
{
// These exceptions often have awful messages that include COM error codes.
// The engine itself may be able to provide a better message.
if (engine.RuntimeErrorMap.TryGetValue(HResult.GetCode(result), out var runtimeErrorMessage) && (runtimeErrorMessage != exception.Message))
{
scriptError = new ScriptEngineException(engine.Name, runtimeErrorMessage, null, HResult.CLEARSCRIPT_E_SCRIPTITEMEXCEPTION, false, false, null, exception.InnerException);
return true;
}
if (engine.SyntaxErrorMap.TryGetValue(HResult.GetCode(result), out var syntaxErrorMessage) && (syntaxErrorMessage != exception.Message))
{
scriptError = new ScriptEngineException(engine.Name, syntaxErrorMessage, null, HResult.CLEARSCRIPT_E_SCRIPTITEMEXCEPTION, false, false, null, exception.InnerException);
return true;
}
}
else if ((result == HResult.DISP_E_MEMBERNOTFOUND) || (result == HResult.DISP_E_UNKNOWNNAME))
{
// this usually indicates invalid object or property access in JScript
scriptError = new ScriptEngineException(engine.Name, "Invalid object or property access", null, HResult.CLEARSCRIPT_E_SCRIPTITEMEXCEPTION, false, false, null, exception.InnerException);
return true;
}
}
else
{
if ((exception is ArgumentException argumentException) && (argumentException.ParamName == null))
{
// this usually indicates invalid object or property access in VBScript
scriptError = new ScriptEngineException(engine.Name, "Invalid object or property access", null, HResult.CLEARSCRIPT_E_SCRIPTITEMEXCEPTION, false, false, null, exception.InnerException);
return true;
}
}
return false;
}