When handling errors locally instead of propagating them, include the error data in logs or messages. Never discard caught errors with `_`, and never silence a failing shell command with only `|| true`; record the failure and its exit status before continuing. Use when writing try/catch, Result handling, error case branches, or shell commands that continue after a failure.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Use Error Info?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/ncaq-use-error-info)
---
name: use-error-info
description: When handling errors locally instead of propagating them, include the error data in logs or messages. Never discard caught errors with `_`, and never silence a failing shell command with only `|| true`; record the failure and its exit status before continuing. Use when writing try/catch, Result handling, error case branches, or shell commands that continue after a failure.
user-invocable: false
---
# エラー処理時にエラーデータを使う
`catch`や`case`で受け取ったエラーを、上流に伝播させずにその場で処理するときは、
受け取ったエラーデータを捨てずに、ログやメッセージに含めるなどして使ってください。
開発者や利用者が問題を把握しやすくするためです。
例えば、原因を修正するのは難しいけれど致命的な問題にはならないエラーは、
警告ログを出力してそのまま続行するのが普通です。
その警告ログにエラーデータの内容を含めます。
エラーデータを`_`などに束縛して、使わずに捨てることは禁止です。
## シェルで失敗を黙って握り潰さない
シェルで失敗しても続行したいコマンドに、
`|| true`や`|| :`だけを付けて失敗を黙って捨てることも禁止です。
受け取る変数が無いだけで、エラーデータを`_`に束縛して捨てるのと同じだからです。
黙って続行すると、後で障害を調べるときに、失敗があったこと自体が分からなくなります。
`2>/dev/null || true`のように、コマンド自身のエラーメッセージまで捨てる書き方も同じです。
何をしようとして失敗したのかと終了コードを、標準エラー出力に残してから続行してください。
systemdのサービスから実行されるスクリプトなら、標準エラー出力はjournalに記録されます。
```bash
# 避ける
systemctl --user stop foo.service || true
# 使う
systemctl --user stop foo.service || echo "foo.serviceの停止に失敗しました (exit=$?)" >&2
```
`||`の右側で展開する`$?`は、左側のコマンドの終了コードになります。
### 失敗ではない終了コード
`grep`で一致する行が無いときの終了コード1のように、
0以外の終了コードが失敗ではなく正常な結果の一つである場合は、ログに残す必要はありません。
その場合も`|| true`で全ての失敗をまとめて捨てずに、想定している終了コードだけを許してください。
`|| true`は、ファイルが読めないような本当の失敗まで隠してしまうためです。
```bash
# grepは一致が無ければ1を、エラーなら2を返すので、1だけを許します。
matches=$(grep foo input.txt) || [ $? -eq 1 ]
```
`mkdir -p`のように、既にその状態になっていることを許すオプションがあるコマンドは、そのオプションを使います。