TL;DR Add
"onFailure": "block"to everyPreToolUse,PermissionRequestorUserPromptSubmithook that exists to stop something, make it exit 0 where it only warns, and test it once with the script renamed. Leave it off formatters, loggers andStophooks.
"onFailure": "block" makes a hook that fails block the action instead of letting it through. Without it, a command, http or mcp_tool hook that can't start, hits its timeout or exits with anything other than 0 or 2 is a non-blocking error: the call goes ahead. A guard with a mistyped path or a crashing script lets everything through, and the only trace is one line in the transcript. If this is your first hook, start with the hooks guide.
What you see when the guard fails
The same PreToolUse hook on Bash, with check-command.js never created, and the same request: Run the Bash command ls -a and nothing else. Interactive sessions on Claude Code v2.1.332 (path shortened to /tmp/demo).
Without onFailure, the hook fails and ls runs:
Listed 1 directory
⎿ PreToolUse:Bash hook failed
Error: Cannot find module
'/tmp/demo/.claude/hooks/check-command.js'
● The ls -a output lists these entries:
With "onFailure": "block", the call never runs:
Listed 1 directory
⎿ Not run · permission denied
│ Reason: PreToolUse:Bash hook error: failed; blocking because onFailure is "block"
● I didn't run ls -a. A PreToolUse hook blocked it before it started.
Headless it's worse: without onFailure, claude -p answered "I received no hook error text", so in a pipeline nobody ever sees the warning.
How to turn it on
1. Add the field to the hook
In the project's .claude/settings.json or your ~/.claude/settings.json:
{
"hooks": {
"PreToolUse": [{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "node",
"args": ["${CLAUDE_PROJECT_DIR}/.claude/hooks/check-command.js"],
"onFailure": "block",
"timeout": 15
}]
}]
}
}
2. Exit 0 where you only warn
With block, an exit 1 counts as a failure even when the hook prints JSON that allows the call. On v2.1.332, a hook that printed "permissionDecision": "allow" and exited 1 blocked ls. If your script exits 1 to warn, switch it to exit 0 and put the warning in systemMessage. To deny, exit 2 or a deny in the JSON, as before; when the rule needs judgment rather than an exit code, see prompt hooks.
3. Lower the timeout
A command hook on PreToolUse defaults to a 600-second timeout: with block, a stuck hook does block, but after ten minutes. With "timeout": 1 and a sleep 5, the error reads timed out; blocking because onFailure is "block".
4. Test it by breaking it
Rename the script and ask Claude for an ls. You should see Not run · permission denied; if ls runs, the field isn't where you think it is.
What counts as a failure
| Case | Without onFailure |
With "block" |
|---|---|---|
| The script is missing or can't start | Goes ahead, with hook failed |
Blocks (tried) |
It hits its timeout |
Goes ahead | Blocks with timed out (tried) |
Exits 1, even if its JSON says allow |
Goes ahead | Blocks (tried) |
http hook with no server listening |
Goes ahead | Blocks with the request failed (ECONNREFUSED) (tried) |
| MCP tool error or server not connected | Goes ahead | Blocks (v2.1.328 or later) |
| JSON that doesn't parse or validate | Goes ahead | Blocks |
Plain text on stdout and exit 0 |
Goes ahead | Goes ahead: not a failure |
Which hooks deserve it
With block, a failure does what exit 2 does on that event, except on PermissionRequest, where it denies the request.
| Hook | block? |
Why |
|---|---|---|
PreToolUse that vetoes commands or paths |
Yes | A failure blocks the call |
PermissionRequest that decides for you |
Yes | A failure denies |
UserPromptSubmit that screens prompts |
Yes | A failure blocks the prompt |
PostToolUse that formats or logs |
No | The tool already ran: Claude only sees the error |
Stop, SubagentStop, TaskCompleted, TeammateIdle |
No effect | Ignored. A Stop hook with block and its script deleted ended the turn normally |
Hooks with async or asyncRewake |
No effect | Ignored |
UserPromptSubmit has one more effect. Since v2.1.299 those hooks run before a skill or slash command expands, so blocking the prompt also stops its embedded shell commands. With a /deploy command holding !`touch shell-ran.txt` , the broken hook without onFailure let the file be created; with block, /deploy answered UserPromptSubmit operation blocked by hook and the file doesn't exist.
For what must never happen, the first wall is still a deny rule in your permissions, because a hook's if filter is best effort, as the conditional hooks tip explains. onFailure keeps the hook behind it from switching off without a word.
The typo that drops the whole file
With "onFailure": "deny", the interactive session stops at startup (output trimmed):
Settings Error
├ hooks.PreToolUse.0.hooks."command": Invalid command hook (onFailure: Invalid option: expected one of "continue"|"block") — a
PreToolUse/PermissionRequest hook that cannot be loaded may be what guards the permissions declared beside it, so nothing it sits in
is applied until the entry is fixed or removed.
Files with errors are skipped entirely, not just the invalid settings.
Pick "Continue without these settings" and that whole settings.json is skipped, hooks and permissions alike. And claude -p shows no dialog: with the same file, ls ran with no hook and no warning. Only "continue" and "block" are valid, lowercase.
On Windows without Git Bash, a shell-form PreToolUse hook with onFailure unset can already block when it fails, and stops doing so the moment you install Git for Windows. Set "onFailure": "block" so the guard behaves the same whatever is installed and, for a PowerShell script, add "shell": "powershell" so it keeps running in PowerShell once Git Bash shows up.
Official docs: Block the action when a hook fails
Requirements
- Claude Code v2.1.295 or later for
commandandhttphooks; v2.1.328 formcp_tool. Tested on v2.1.332, on Linux, withclaude -pand an interactive session.