How to make Zsh not store failed command

Bart Schaefer proposed the following approach to the same question on the zsh users mailing list:

 zshaddhistory() { whence ${${(z)1}[1]} >| /dev/null || return 1 }

This function is executed before the command line is written to history. If it does return 1, the current command line is neither appended to the history file nor to the local history stack. However, the check if the command will trigger a command not found error covers only simple cases. E.g. this line will be on the history:

echo foo; echooo bar

But it works fine for your example

gut status

Please be aware, that the wrong command will show up upon UP-ARROW (so that you can correct it!), but is not in the history stack; check with fc -l.