Improve error handling in the undef middleware