Fixed error handling middleware