class PardotAPIError(Exception):
    """
    Basic exception class for errors encountered in API post and get requests. Takes the json response and parses out
    the error code and message.
    """

    def __init__(self, json_response):
        self.response = json_response
        try:
            self.err_code = json_response['@attributes']['err_code']
            self.message = str(json_response['err'])
        except KeyError:
            self.err_code = 0
            self.message = 'Unknown API error occurred'

    def __str__(self):
        return 'Pardot API Error {err_code}: {message}'.format(err_code=self.err_code, message=self.message)

class PardotAPIError(Exception):
    """
    Basic exception class for errors encountered in API post and get requests. Takes the json response and parses out
    the error code and message.
    """

    def __init__(self, json_response):
        self.response = json_response
        self.err_code = json_response.get('@attributes').get('err_code')
        self.message = str(json_response.get('err'))
        if self.err_code is None:
            self.err_code = 0
            self.message = 'Unknown API error occurred'

    def __str__(self):
        return 'Pardot API Error {err_code}: {message}'.format(err_code=self.err_code, message=self.message)

Refactor error data extraction from JSON
Refactor error data extraction from JSON

  class PardotAPIError(Exception):
      """
      Basic exception class for errors encountered in API post and get requests. Takes the json response and parses out
      the error code and message.
      """
  
      def __init__(self, json_response):
          self.response = json_response
-         try:
-             self.err_code = json_response['@attributes']['err_code']
+         self.err_code = json_response.get('@attributes').get('err_code')
-             self.message = str(json_response['err'])
+         self.message = str(json_response.get('err'))
-         except KeyError:
+         if self.err_code is None:
              self.err_code = 0
              self.message = 'Unknown API error occurred'
  
      def __str__(self):
          return 'Pardot API Error {err_code}: {message}'.format(err_code=self.err_code, message=self.message)
  
Refactor error data extraction from JSON
## Code Before:
class PardotAPIError(Exception):
    """
    Basic exception class for errors encountered in API post and get requests. Takes the json response and parses out
    the error code and message.
    """

    def __init__(self, json_response):
        self.response = json_response
        try:
            self.err_code = json_response['@attributes']['err_code']
            self.message = str(json_response['err'])
        except KeyError:
            self.err_code = 0
            self.message = 'Unknown API error occurred'

    def __str__(self):
        return 'Pardot API Error {err_code}: {message}'.format(err_code=self.err_code, message=self.message)

## Instruction:
Refactor error data extraction from JSON
## Code After:
class PardotAPIError(Exception):
    """
    Basic exception class for errors encountered in API post and get requests. Takes the json response and parses out
    the error code and message.
    """

    def __init__(self, json_response):
        self.response = json_response
        self.err_code = json_response.get('@attributes').get('err_code')
        self.message = str(json_response.get('err'))
        if self.err_code is None:
            self.err_code = 0
            self.message = 'Unknown API error occurred'

    def __str__(self):
        return 'Pardot API Error {err_code}: {message}'.format(err_code=self.err_code, message=self.message)
