normalize API errors, and catch them in a middleware