Catch errors from APIs and return error code