plugins/languages/powershell/skills/error/SKILL.md
PowerShell error handling: terminating vs non-terminating errors, $ErrorActionPreference levels (Continue/Stop/SilentlyContinue/Ignore/Inquire), -ErrorAction parameter scope, try/catch/finally with typed catches, ErrorRecord anatomy ($_.Exception/$_.CategoryInfo/$_.TargetObject), throw vs Write-Error, trap statement, $LASTEXITCODE for native commands, $PSNativeCommandUseErrorActionPreference (7.3+), $? boolean, exit codes, custom exception classes. Use proactively when the user asks "PowerShell 错误处理 / try-catch / ErrorAction / 退出码 / LASTEXITCODE / terminating error". Also triggers on "throw", "trap", "$Error", "ErrorRecord", "$ErrorActionPreference", "终止错误", "非终止错误".
npx skillsauth add lazygophers/ccplugin powershell-errorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
PowerShell 错误分两类,行为差异是大多 bug 的根因。
| 类型 | 触发 | 默认行为 | 能 catch? |
|------|------|---------|-----------|
| Terminating | throw / [CmdletBinding()] 致命错误 / $ErrorActionPreference='Stop' | 中断管道 | ✅ |
| Non-terminating | Write-Error / cmdlet 局部失败 | 写入 $Error,继续 | ❌(除非 -ErrorAction Stop) |
$ErrorActionPreference$ErrorActionPreference = 'Stop' # 推荐脚本默认
| 值 | 行为 |
|----|------|
| Continue(默认) | 打印错误,继续 |
| Stop | 转 terminating,可 catch |
| SilentlyContinue | 静默,仅记 $Error |
| Ignore | 不记 $Error,慎用 |
| Inquire | 询问(交互式) |
| Break (7.0+) | 进调试器 |
作用域:脚本顶层设置仅影响当前脚本;模块函数内独立。
$PSNativeCommandUseErrorActionPreference = $true
启用后,git、cmake、pytest 等原生命令非零 $LASTEXITCODE 也会触发 $ErrorActionPreference。强烈推荐脚本头部启用。
try {
$data = Invoke-RestMethod -Uri $url -ErrorAction Stop
Process-Data $data
}
catch [System.Net.Http.HttpRequestException] {
Write-Warning "Network: $($_.Exception.Message)"
return
}
catch [System.IO.IOException] {
Write-Warning "IO: $($_.Exception.Message)"
throw # 重新抛出
}
catch {
Write-Error "Unexpected: $_"
throw
}
finally {
Cleanup-Resources
}
类型化 catch 块按顺序匹配;最后的无类型 catch 兜底。$_ 是 ErrorRecord。
catch {
$_.Exception.Message # 异常文本
$_.Exception.GetType().FullName
$_.CategoryInfo # 分类(NotSpecified / InvalidArgument...)
$_.FullyQualifiedErrorId # 唯一 ID(适合自动化匹配)
$_.TargetObject # 出错对象
$_.ScriptStackTrace # 调用栈
$_.InvocationInfo.PositionMessage # 行号 / 上下文
}
# 简单
throw "Invalid input: $name"
# 强类型(推荐)
throw [System.ArgumentException]::new('name cannot be empty', 'Name')
# 自定义异常类(PowerShell 5+ class)
class MyAppException : System.Exception {
MyAppException([string]$m) : base($m) { }
}
throw [MyAppException]::new('boom')
# 在 advanced function 内:ThrowTerminatingError 给调用方更好的栈帧
$err = [System.Management.Automation.ErrorRecord]::new(
[InvalidOperationException]::new('failed'),
'MyApp.Failed',
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$null
)
$PSCmdlet.ThrowTerminatingError($err)
# Write-Error:非终止,调用方可决定是否升级
Write-Error -Message 'soft warn' -Category InvalidData -ErrorId 'MyApp.SoftWarn'
trap {
Write-Warning "caught: $_"
continue # 跳过当前语句继续
}
新代码用 try/catch;trap 是早期遗物,作用域语义令人困惑。
$LASTEXITCODE 与原生命令git status
if ($LASTEXITCODE -ne 0) {
throw "git failed: $LASTEXITCODE"
}
# 推荐 7.3+ 启用 $PSNativeCommandUseErrorActionPreference 后这步可省
$? 与 $ErrorSome-Cmdlet
if (-not $?) { Write-Warning 'last command failed' }
$Error[0] # 最近一次错误
$Error.Clear() # 清空
$?只是布尔,信息少;优先try/catch。
Get-Item /nope -ErrorAction Stop # 这一调用变 terminating
Get-Item /nope -ErrorAction SilentlyContinue # 静默
Get-Item /nope -ErrorVariable myErr # 捕获到变量(不抑制)
Get-Item /nope -ErrorAction Stop -ErrorVariable +allErrs # 累积
exit 0 # 成功
exit 1 # 通用失败
exit 2 # 用法错误(约定俗成)
调用方通过 $LASTEXITCODE(pwsh)或 $? / %ERRORLEVEL%(cmd)读取。脚本应文档化退出码语义。
$ErrorActionPreference = 'Stop'$PSNativeCommandUseErrorActionPreference = $truethrow 或包装抛出(保留原异常)-ErrorAction Stop$PSCmdlet.ThrowTerminatingError,不用 throw "string"$LASTEXITCODE(或用 preference 变量自动)"failed"FullyQualifiedErrorId 唯一,便于自动化匹配tools
--- name: trellisx-workspace description: 维护 `.trellis/task.md` 任务看板 —— trellis 缺的跨任务总览。**一个表格, 一行一个任务**, 列为 id/名称/描述/状态/阶段/进度/worktree (状态/阶段中文显示)。在 task create/start/阶段切换/archive 后**及时更新**对应行; 并**自动清理超 7 天的已完成行**防膨胀。保持看板与 task.json 实时一致。 when_to_use: 维护 / 创建 / 更新 `.trellis/task.md` 任务看板时; task 生命周期任一节点 (create/start/阶段推进/archive) 之后同步看板时; 用户问"当前有哪些任务 / 任务进度 / 任务看板"时。被 trellisx-flow 与 trellisx-apply 注入的流程引用。 user-invocable: true argument-hint: [show|update|sync|cleanup ...] [task id] arguments:
testing
强制以 Trellis task 闭环处理用户指定的请求 (自判新建/并入 → plan→exec→check→finish 全程不跳步)。**仅用户显式主动调用** (/trellisx-flow 或明确要求"强制走 task 处理这个"); **禁止自动 / 被动 / 推断式调用** —— 不要因为某个请求"看起来该建 task"就自动触发本 skill, 那是 apply 注入的 no_task 倾向的职责。
testing
把 强推task + subtask拆分 + worktree隔离 + 闭环收尾 四维度增量注入当前项目 .trellis/ (workflow.md 的 no_task/planning/in_progress 块 + spec 背书文档 + trellis 生命周期 hook worktree 自动化)。强推 task 与闭环为纯 prompt 软约束 (非平台 hook 硬拦截)。**纯增量追加, 绝不替换 trellis 原生文本** (no_task 分类+征同意/check/finish/前缀全保留)。幂等 (marker 包裹)。
development
Claude Code 会话历史整理 — 扫 ~/.claude/projects/**/*.jsonl 全部 session transcripts, 提取学习增量 (用户校正/决策/踩坑/L0 规则) → 全局记忆库 ~/.cortex/.wiki/memory/. 默认 --apply 落盘 (--dry-run opt-in 仅出 JSON plan 预览). 与 cortex-extract (L4-inbox 内部) 互补.