grepwill generate a non-zero exit code if the template does not match. Moreover, he did not give any error messages. To get an error message if the template is not found at the input, you will need an alternative. You can use awk:
echo -e "foo \n bar" | \
awk 'BEGIN{f=0}/baz/{f=1;print;}END{if (!f) {print "Error; string not found"; exit 1;}}' | \
tr -d ' '
This will be very similar grep(in terms of exit code) and will generate an error message if no match is found.
STDERR, :
echo -e "foo \n bar" | \
awk 'BEGIN{f=0}/baz/{f=1;print;}END{if (!f) {print "Error; string not found" > "/dev/stderr"; exit 1;}}' | \
tr -d ' '