Correctly handle errors in template