code: def handle_api_error(resp):
      
      content = yield resp.json()
  
      headers = HeaderWrapper(resp.headers)
  
      try:
          err = content['error']
      except (KeyError, TypeError):
          raise error.APIError(
              "Invalid response object from API: %r (HTTP response code "
              "was %d)" % (content, resp.code),
              resp, resp.code, content, headers)
  
      if resp.code in [400, 404]:
          raise error.InvalidRequestError(
              err.get('message'), err.get('param'),
              resp, resp.code, content, headers)
      elif resp.code == 401:
          raise error.AuthenticationError(
              err.get('message'),
              resp, resp.code, content, headers)
      elif resp.code == 402:
          raise error.CardError(
              err.get('message'), err.get('param'), err.get('code'),
              content, resp.code, resp, headers)
      else:
          raise error.APIError(
              err.get('message'), content, resp.code, resp, headers)
docs: Stolen straight from the Stripe Python source.