Ensure proper error object when middleware reports an error