better error handling for path manipulating middleware