Esempio n. 1
0
    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 _check_response_status(self, response, prefix):
     ''' Check for status OK 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:
             CvpRequestError: A CvpRequestError is raised if request
             response status is not OK.
     '''
     if not response.ok:
         msg = '%s: Request Error: %s - %s' % (prefix, response.reason,
                                               response.text)
         self.log.error(msg)
         raise CvpRequestError(msg)
Esempio n. 3
0
    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)