skip to content

A Claude Code PreToolUse guard prints an error and exits 1 on terraform destroy, yet the command still runs. Why, and what do you change?

level: middleimportance: must knowfreq 18%

answer

  1. the Unix habit is wrong here
  2. non-blocking error notice
  3. only one code blocks
  4. or deny through JSON

basics

~20 s

Claude Code treats exit 1 as a non-blocking error, so the call proceeds with only a hook-error notice. Exit 2 with the reason on stderr, or exit 0 and print JSON with permissionDecision set to deny.

solid answer

~40 s

For most Claude Code hook events, **exit 2 is the only code that blocks through the code alone**. Exit 1, the conventional Unix failure, is a **non-blocking error**: without valid JSON on stdout, the tool call proceeds and the transcript shows a `<hook name> hook error` notice with the first line of stderr after `Failed with non-blocking status code:`. Fix it in one of two ways: write the reason to stderr and `exit 2`, which blocks the call and shows Claude that text as the denial reason, or exit 0 and print `{"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "deny", "permissionDecisionReason": "..."}}`. Then check the other fail-open paths: a mistyped script path (exit 127), a hook that hits its `timeout`, malformed JSON and a missing `jq` all let the call through.

code

bash · 9 lines
bash
#!/bin/bash
# PreToolUse hook, matcher "Bash"
input=$(cat)
cmd=$(jq -r '.tool_input.command // ""' <<<"$input")
if [[ "$cmd" == *"terraform destroy"* ]]; then
  echo "Blocked: run terraform destroy yourself after review" >&2
  exit 2   # exit 1 here would let the command run
fi
exit 0     # no decision: the normal permission flow applies

go deeper

for a junior

Recall the one rule: in Claude Code, exit 2 blocks and exit 1 does not. A guard that exits 1 only produces a hook error notice while the command runs.

for a middle

Explain the full table: exit 0, exit 2 with stderr as the reason, other codes as non-blocking errors, and how schema-valid JSON on stdout decides regardless of a code other than 2.

for a senior

Show you hunt every fail-open path: bad script paths, timeouts, unparseable JSON, profile output before the brace, misplaced fields and missing jq, and that you test a guard against a command it must block.

for a principal

Frame a hook as one enforcement layer that fails open by design, and decide which rules belong in permission deny rules or the sandbox instead, where a crashed script cannot quietly disable them.

## What the guard actually did The guard did detect `terraform destroy`, printed a message and exited with status **1**, which is what most shell scripts do on failure. In Claude Code that status doesn't mean "stop". For most events, **any exit code other than 0 and 2 is a non-blocking error**: the action proceeds, and the transcript shows a `<hook name> hook error` notice whose text is the first line of stderr after `Failed with non-blocking status code:`. The guard reported a problem and then let the command run. The docs state the rule plainly: exit code 2 is the only exit code that blocks through the code alone, and exit 1 is a non-blocking error even though it is the conventional Unix failure code. If a hook is meant to enforce a policy, it must use `exit 2` or a JSON decision. ## The exit-code rule | What the hook does | Outcome on `PreToolUse` | |---|---| | `exit 0`, nothing on stdout | No decision; the normal permission flow applies | | `exit 2`, reason on stderr | **Blocked**; Claude sees stderr as the denial reason | | `exit 1`, text on stderr only | Proceeds; hook error notice in the transcript | | `exit 1`, schema-valid JSON on stdout | The JSON decides, so a `deny` still denies | | Exit 0 with JSON that fails to parse | Proceeds; non-blocking error with the parse message | The fourth row matters: Claude Code reads JSON output on every exit code, and with a schema-valid object and a code other than 2, it ignores the code and lets the JSON decide. The trap is specifically the script that prints plain text, or nothing, and exits 1. ## Two correct ways to block 1. **Exit 2 with stderr.** Write the reason to stderr and `exit 2`. The call is blocked, and Claude reads the stderr text as the denial reason, so it can choose another approach. This is the simplest fix for a yes/no guard. 2. **Exit 0 with a JSON decision.** Print one JSON object whose `hookSpecificOutput` holds `hookEventName: "PreToolUse"`, `permissionDecision: "deny"` and a `permissionDecisionReason`. This is the route when the hook may also want to `ask`, `allow` or add context. Don't mix the two casually: if you exit 2, the block holds even when the JSON says `allow`. ## Other ways a guard fails open - **The script never starts.** A mistyped path in `settings.json`, or a script without the executable bit, makes the shell exit with a code such as 127. That lands in the same non-blocking bucket, so the action proceeds. Watch for the notice on a policy hook's first run. - **The hook times out.** Claude Code cancels a command hook at its `timeout` (600 seconds by default for command hooks) and discards its output. On `PreToolUse` the call then continues through the normal permission flow, so a stalled guard is not a gate. - **The JSON doesn't parse or validate.** Output that looks like JSON but isn't valid, or valid JSON that fails the schema, is a non-blocking error on exit codes other than 2. - **Stdout is polluted.** A shell profile that echoes a banner puts text before the `{`, so the whole output is treated as plain text and the decision is silently ignored. - **A field is misplaced.** `permissionDecision` at the top level instead of inside `hookSpecificOutput` is ignored without an error; the debug log lists it under `Hook JSON output had unrecognized keys`. - **A dependency is missing.** If `jq` isn't on `PATH`, `cmd=$(jq ...)` comes back empty, the pattern never matches and the script reaches `exit 0`. ## Hardening checklist 1. Block with `exit 2` or a JSON `deny`, never with `exit 1`. 2. Test the guard with a command that must be blocked and confirm Claude reports the denial reason. 3. Keep the check fast and set a deliberate `timeout`. 4. Build JSON with an encoder such as `jq -n` rather than string concatenation. 5. Run `claude --debug-file <path>` while testing to see each hook's exit code, stdout and stderr. A Claude Code hook is not a git hook, and its exit statuses follow their own rule; the convention git uses for its hooks is a separate subject.

  • The guard prints a schema-valid JSON deny and exits 1. Does the call run?
    No. Claude Code reads JSON output on every exit code. For events that use the standard decision model, a schema-valid object with an exit code other than 2 means the code is ignored and the JSON decides, so `permissionDecision: "deny"` denies the call. The exit-1 trap is the script that prints only plain text or nothing.
  • What does a timed-out PreToolUse command hook do to the pending call?
    Claude Code cancels the hook at its `timeout`, 600 seconds by default for a command hook, and discards its output, so the hook renders no decision. The call continues through the normal permission flow. A guard that calls a slow service fails open on a timeout, so keep it fast or put the hard rule in a permission rule instead.
  • How do you confirm a new policy hook is actually enforcing?
    Trigger it with a command it must block and check that Claude reports the denial reason. Watch for a `<hook name> hook error` notice, which means the hook failed open, for example on a bad path. Start Claude Code with `--debug-file` to see each hook's exit code, stdout and stderr.

Exit 1 is a smoke detector chirping about a low battery: it makes a noise but opens no sprinkler. Only exit 2, or an explicit deny decision, actually trips the system and stops the action.

  • Any non-zero exit status blocks the tool call, like a failing CI step
  • Exit 1 is the right blocking code because it is the Unix failure convention
  • A guard that times out blocks the call to be safe
  • If the hook script path is wrong, Claude Code refuses to run the tool
  • Printing the reason to stdout as plain text is enough for Claude to see it