catch errors that happen in middleware