1

I am using a package called lob to standardize a dataset of addresses I have. I have been receiving a 504 gateway error after running through a few thousand addresses. The response error from Lob is the following:

.......Traceback (most recent call last):
  File "verify_modified_v2.py", line 82, in <module>
    zip_code=row['zip_code'],
  File "C:\Users\******\Anaconda2\lib\site-packages\lob\resource.py", line 123, in create
    response = requestor.request('post', cls.endpoint, params)
  File "C:\Users\******\Anaconda2\lib\site-packages\lob\api_requestor.py", line 84, in request
    requests.post(lob.api_base + url, auth=(self.api_key, ''), data=data, files=files, headers=headers)
  File "C:\Users\******\Anaconda2\lib\site-packages\lob\api_requestor.py", line 27, in parse_response
    resp.content, resp.status_code, resp)
lob.error.APIConnectionError: {
    "error": {
        "message": "GATEWAY_TIMEOUT",
        "status_code": 504
    }
}

I have tried to except this error so that my code can repeatedly contact lob until it can get through without the gateway error:

for idx, row in enumerate(input_csv):
        try:
            verifiedAddress = lob.USVerification.create(
                primary_line=row['primary_line'],
                secondary_line=row['secondary_line'],
                city=row['city'],
                state=row['state'],
                zip_code=row['zip_code'],
            )

            if verifiedAddress.deliverability in success_deliverabilities:
                success_csv.writerow({
                    'primary_line': verifiedAddress.primary_line,
                    'secondary_line': verifiedAddress.secondary_line,
                    'urbanization':  verifiedAddress.urbanization,
                    'last_line':  verifiedAddress.last_line,
                    'deliverability': verifiedAddress.deliverability,
                    'identifier': row['identifier'],
                    'status': row['2']
                })
            else:
                failure_csv.writerow({
                    'primary_line': row['primary_line'],
                    'secondary_line': row['secondary_line'],
                    'city':  row['city'],
                    'state':  row['state'],
                    'zip_code':  row['zip_code'],
                    'deliverability': verifiedAddress.deliverability,
                    'identifier': row['identifier'],
                    'status': row['2']
                })

            # Print success
            sys.stdout.write('.')
            sys.stdout.flush()

             # New lines for larger csv's
            if idx % 10 is 9:
                sys.stdout.write('\n')
                sys.stdout.flush()

        except lob.error.APIConnectionError:
            print "caught error"

It does not seem that the gateway error is able to be "excepted"; does anyone have any thoughts on a way around this?

My end goal is this:

  1. Bypass the error.
  2. Log the error.
  3. Continue with the next row of the csv file.

Thanks.

Phil
  • 11
  • 1
  • 4
  • Don't you need an `except` after `try`? – BallpointBen Jun 08 '17 at 20:37
  • The except is at the bottom of the code block – Phil Jun 09 '17 at 14:59
  • Hmm, my eyes must be playing tricks on me, could have sworn that said `finally`. Anyway, what are you seeing? – BallpointBen Jun 09 '17 at 15:04
  • @ballpointben I'm at a bit of a loss. I'm not sure how to format my code so that when I receive this response from lob it will continue running: `lob.error.APIConnectionError: { "error": { "message": "GATEWAY_TIMEOUT", "status_code": 504 } }` – Phil Jun 09 '17 at 15:26
  • Change that line to `except Exception:`; now what happens? – BallpointBen Jun 09 '17 at 15:27
  • Running it now. It looks like that is working actually, thanks @ballpointben Will report back once it claims entire csv has been ran through the script. – Phil Jun 09 '17 at 15:56
  • In general you should be more specific than `except Exception`; I only proposed that as a debugging step. I’m still not sure what the underlying issue is. – BallpointBen Jun 09 '17 at 15:57
  • Yes it helped with debugging, just got through an enormous csv file. I'm working on how to display the specific error that the code excepts. Any thoughts? – Phil Jun 11 '17 at 01:58
  • `except Exception as e:` ... `print(e)` – BallpointBen Jun 11 '17 at 02:01

0 Answers0