Improve error message and optimize record macros
Improve doctest error messages, closes #1318