def _is_good_response(self, response, prefix): ''' Check for errors in a response from a GET or POST request. The response argument contains a response object from a GET or POST request. The prefix argument contains the prefix to put into the error message. Raises: CvpApiError: A CvpApiError is raised if there was a JSON error. CvpRequestError: A CvpRequestError is raised if the request is not properly constructed. CvpSessionLogOutError: A CvpSessionLogOutError is raised if response from server indicates session was logged out. ''' if not response.ok: if 'Unauthorized' in response.reason: # Check for 'Unauthorized' User error because this is how # CVP responds to a logged out users requests in 2018.x. msg = '%s: Request Error: %s' % (prefix, response.reason) self.log.error(msg) raise CvpApiError(msg) if 'User is unauthorized' in response.text: # Check for 'User is unauthorized' response text because this # is how CVP responds to a logged out users requests in 2019.x. msg = '%s: Request Error: User is unauthorized' % prefix self.log.error(msg) raise CvpApiError(msg) else: msg = '%s: Request Error: %s - %s' % (prefix, response.reason, response.text) self.log.error(msg) raise CvpRequestError(msg) if 'LOG OUT MESSAGE' in response.text: msg = ('%s: Request Error: session logged out' % prefix) raise CvpSessionLogOutError(msg) joutput = json_decoder(response.text) err_code_val = self._finditem(joutput, 'errorCode') if err_code_val: if 'errorMessage' in joutput: err_msg = joutput['errorMessage'] else: if 'errors' in joutput: error_list = joutput['errors'] else: error_list = [joutput['errorCode']] # Build the error message from all the errors. err_msg = error_list[0] for idx in range(1, len(error_list)): err_msg = '%s\n%s' % (err_msg, error_list[idx]) msg = ('%s: Request Error: %s' % (prefix, err_msg)) self.log.error(msg) raise CvpApiError(msg)
def test_make_request_response_error_logout(self): """ Test request exception raised if CVP logout error hit. """ self.clnt.session = Mock() self.clnt.session.return_value = True self.clnt.session.get.return_value = Mock() self.clnt._create_session = Mock() self.clnt._reset_session = Mock() self.clnt.NUM_RETRY_REQUESTS = 2 self.clnt.connect_timeout = 2 self.clnt.node_cnt = 2 self.clnt.url_prefix = 'https://1.1.1.1:7777/web' self.clnt._is_good_response = Mock() self.clnt._is_good_response.side_effect = CvpSessionLogOutError('bad') self.assertIsNone(self.clnt.last_used_node) with self.assertRaises(CvpSessionLogOutError): self.clnt._make_request('GET', 'url', 2, {'data': 'data'}) self.assertEqual(self.clnt.last_used_node, '1.1.1.1')
def _is_good_response(self, response, prefix): ''' Check for errors in a response from a GET or POST request. The response argument contains a response object from a GET or POST request. The prefix argument contains the prefix to put into the error message. Raises: CvpApiError: A CvpApiError is raised if there was a JSON error. CvpRequestError: A CvpRequestError is raised if the request is not properly constructed. CvpSessionLogOutError: A CvpSessionLogOutError is raised if response from server indicates session was logged out. ''' if not response.ok: msg = '%s: Request Error: %s' % (prefix, response.reason) self.log.error(msg) raise CvpRequestError(msg) if 'LOG OUT MESSAGE' in response.text: msg = ('%s: Request Error: session logged out' % prefix) raise CvpSessionLogOutError(msg) if 'errorCode' in response.text: joutput = response.json() if 'errorMessage' in joutput: err_msg = joutput['errorMessage'] else: if 'errors' in joutput: error_list = joutput['errors'] else: error_list = [joutput['errorCode']] # Build the error message from all the errors. err_msg = error_list[0] for idx in range(1, len(error_list)): err_msg = '%s\n%s' % (err_msg, error_list[idx]) msg = ('%s: Request Error: %s' % (prefix, err_msg)) self.log.error(msg) raise CvpApiError(msg)