add function to handling error in middleware