refactors error handling middleware to the global middleware module