Use request attribute if available for error message

Fixes gh-1762, gh-1731