Fix error handling middleware