Improved documentation and error handling

This ensures that things like setting null headers as reported in #1403 are
easier to debug.