add test and more helpful error message

This diagnoses #2442. Also makes it possible to debug your templates.