babalae/better-genshin-impact · error · Exception

JavaScript execution error: {ex.Message}

Error message

JavaScript execution error: {ex.Message}

What it means

Wraps a ClearScript ScriptEngineException thrown during engine.Execute(javaScript). ScriptEngineException fires on JS syntax errors, runtime errors (TypeError, ReferenceError), or host-call failures. The wrapper flattens it into a generic System.Exception carrying the engine's message and inner exception.

Source

Thrown at BetterGenshinImpact/GameTask/AutoArtifactSalvage/AutoArtifactSalvageTask.cs:502

            {
                throw new InvalidOperationException("JavaScript没有设置Output输出");
            }

            if (engine.Script.Output is not bool)
            {
                throw new InvalidOperationException("JavaScript的Output输出不是布尔类型");
            }

            return (bool)engine.Script.Output;
        }
        catch (ScriptInterruptedException)
        {
            logger.LogWarning("脚本执行超出3秒限制,请使用正确的JS代码(JavaScript execution timeout!)");
            throw;
        }
        catch (ScriptEngineException ex)
        {
            throw new Exception($"JavaScript execution error: {ex.Message}", ex);
        }
    }

    public static bool IsMatchRegularExpression(string affixes, string regularExpression, out string msg)
    {
        Match match = Regex.Match(affixes, regularExpression);
        if (match.Success)
        {
            if (string.IsNullOrEmpty(match.Value))
            {
                msg = "匹配成功!";
            }
            else
            {
                msg = $"匹配成功:{match.Value}";
            }
        }
        else

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Read ex.Message (and the inner ScriptEngineException.ErrorDetails if available) to find the JS line/column.
  2. Validate/lint the JS before passing it to IsMatchJavaScript.
  3. Wrap risky host interactions in try/catch inside the JS.
  4. Provide a documented set of available globals (ArtifactStat) and a template.

Example fix

// before
await Task.Run(() => engine.Execute(javaScript));
catch (ScriptEngineException ex) { throw new Exception($"JavaScript execution error: {ex.Message}", ex); }
// after - surface line info
catch (ScriptEngineException ex)
{
    throw new Exception($"JavaScript execution error: {ex.Message}\n{ex.ErrorDetails}", ex);
}
Defensive patterns

Strategy: try-catch

Validate before calling

try { Jint.Engine e = new(); e.Execute(js); } catch { /* reject before passing to ClearScript */ }

Try / catch

try { await Task.Run(() => engine.Execute(javaScript)); }
catch (ScriptEngineException ex) { throw new Exception($"JavaScript execution error: {ex.ErrorDetails ?? ex.Message}", ex); }

Prevention

When it happens

Trigger: JS has a syntax error (missing brace, bad token); references an undefined variable; calls a host method that throws; uses unsupported ES features for the V8 version bundled with ClearScript.

Common situations: User-typed JS with typos; script assumes a global (like 'console' or 'require') not provided by ClearScript; version drift in the ClearScript/V8 engine changing supported syntax.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/e2fbdc15af23c45c. Report an issue: GitHub.