{"record":{"id":"94fa2e41436345c9","repo":"JosefNemec/Playnite","slug":"e-message","errorCode":null,"errorMessage":"e.Message","messagePattern":"e\\.Message","errorType":"exception","errorClass":"ScriptRuntimeException","httpStatus":null,"severity":"error","filePath":"source/Playnite/Scripting/PowerShell/PowerShell.cs","lineNumber":206,"sourceCode":"                using (var pipe = runspace.CreatePipeline(script))\r\n                {\r\n                    if (variables != null)\r\n                    {\r\n                        foreach (var key in variables.Keys)\r\n                        {\r\n                            runspace.SessionStateProxy.SetVariable(key, variables[key]);\r\n                        }\r\n                    }\r\n\r\n                    Collection<PSObject> result = null;\r\n\r\n                    try\r\n                    {\r\n                        result = pipe.Invoke();\r\n                    }\r\n                    catch (RuntimeException e)\r\n                    {\r\n                        throw new ScriptRuntimeException(e.Message, e.ErrorRecord.ScriptStackTrace);\r\n                    }\r\n\r\n                    if (result == null)\r\n                    {\r\n                        return null;\r\n                    }\r\n                    else\r\n                    {\r\n                        if (result.Count == 1)\r\n                        {\r\n                            return result[0]?.BaseObject;\r\n                        }\r\n                        else\r\n                        {\r\n                            return result.Select(a => a?.BaseObject).ToList();\r\n                        }\r\n                    }\r\n                }\r","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/JosefNemec/Playnite/blob/5911f4e964e628aa7a69c2030ab35101afa63067/source/Playnite/Scripting/PowerShell/PowerShell.cs#L188-L224","documentation":"Re-thrown from PowerShellRuntime.Execute when pipe.Invoke() raises a System.Management.Automation.RuntimeException. The catch wraps the PS error message and ScriptStackTrace into a ScriptRuntimeException, preserving PowerShell's diagnostic detail for the caller to surface.","triggerScenarios":"PowerShellRuntime.Execute(script, ...) runs a script via runspace.CreatePipeline(script).Invoke(); the script throws at runtime (cmdlet not found, type error, divide-by-zero, null dereference, etc.), producing a RuntimeException that the catch at line 204 wraps.","commonSituations":"Extension/automation script calls an undefined cmdlet or function, references $null members, uses a type not loaded, or violates ErrorActionPreference='Stop' set at line 115. A script written for newer PowerShell API calls missing on the user's machine. Bad variable passed in via the variables dictionary.","solutions":["Read ScriptRuntimeException.ScriptStackTrace to locate the failing line/command in the user script.","Run the script standalone in a PowerShell host with the same variables to reproduce, then fix the offending cmdlet/type/null access.","Ensure any module the script depends on is Import-Module'd before execution.","Set ErrorActionPreference appropriately or wrap fragile regions in try/catch within the PS script."],"exampleFix":"// caller pattern (already used in MainViewModelBase.RunAppScript):\ntry\n{\n    runtime.Execute(script, workDir, vars);\n}\ncatch (ScriptRuntimeException ex)\n{\n    Logger.Error(ex, $\"Script failed: {ex.Message}\\n{ex.ScriptStackTrace}\");\n}","handlingStrategy":"try-catch","validationCode":"// Validate the script compiles/imports before executing user content where possible:\nif (string.IsNullOrWhiteSpace(script)) return;\n// (full pre-validation of arbitrary PS is impractical — rely on try/catch at execution)","typeGuard":"static bool LooksLikeValidScript(string s) => !string.IsNullOrWhiteSpace(s) && !s.Contains(\"\\0\");","tryCatchPattern":"try { runtime.Execute(script, workDir, vars); }\ncatch (ScriptRuntimeException ex)\n{\n    Logger.Error($\"PS script failed: {ex.Message}\\n{ex.ScriptStackTrace}\");\n    Dialogs.ShowMessage($\"Script error:\\n{ex.Message}\\n\\n{ex.ScriptStackTrace}\", image: MessageBoxImage.Error);\n}","preventionTips":["Always surface ScriptRuntimeException.ScriptStackTrace to the user for debugging.","Test extension scripts standalone in a PowerShell host before shipping.","Set ErrorActionPreference deliberately; wrap fragile regions in PS try/catch."],"tags":["scripting","powershell","runtime","playnite"],"backgroundTag":null,"analyzedSha":"5911f4e964e628aa7a69c2030ab35101afa63067","analyzedAt":"2026-08-13T17:38:25.713Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}