Correctly catch template errors without returning invalid data

Fixes #6329