简体   繁体   中英

Python: Why do is the traceback getting printed?

I have a function. The function get started in some more threads. I tried to print a own error message. But its not important what I do I get still the traceback printed. My function:

def getSuggestengineResultForThree(suggestengine, seed, dynamoDBLocation):
    results[seed][suggestengine] = getsuggestsforsearchengine(seed, suggestengine)

    for keyword_result in results[seed][suggestengine]:
        o = 0
        while True:
            try:
                allKeywords.put_item(
                    Item={
                        'keyword': keyword_result
                    }
                )
                break
            except ProvisionedThroughputExceededException as pe:
                if (o > 9):
                    addtoerrortable(keyword_result)
                    print('ProvisionedThroughputExceededException 10 times in getSuggestengineResultForThree for allKeywords')
                    break
                sleep(1)
                o = o + 1
                print("ProvisionedThroughputExceededException in getSugestengineResult")

But I get for every Thread an output like this:

Exception in thread Thread-562:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
  File "/Users/iTom/ownCloud/Documents/Workspace/PyCharm/Keywords/TesterWithDB.py", line 362, in getSuggestengineResultForThree
'keyword': keyword_result
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/boto3/resources/factory.py", line 518, in do_action
response = action(self, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/botocore/client.py", line 252, in _api_call
return self._make_api_call(operation_name, kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/botocore/client.py", line 542, in _make_api_call
raise ClientError(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ProvisionedThroughputExceededException) when calling the PutItem operation: The level of configured provisioned throughput for the table was exceeded. Consider increasing your provisioning level with the UpdateTable API

Can someone help me to get my own print instead of the traceback? :)

This answer is a bit late for your question but here it is in case anyone is searching for this answer.

The exception that boto3 throws is a botocore.exceptions.ClientError as Neil has answered. However you should check response error code for 'ProvisionedThroughputExceededException' because the ClientError could be for another issue.

from botocore.exceptions import ClientError

except ClientError as e:
  if e.response['Error']['Code'] != 'ProvisionedThroughputExceededException':
    raise
  # do something else with 'e'

I am using Python 2.7 which may or may not make a difference. The exception that I receive suggests that boto3 is already doing retries (up to 9 times) which is different from your exception:

An error occurred (ProvisionedThroughputExceededException) when calling the PutItem operation (reached max retries: 9): The level of configured provisioned throughput for the table was exceeded. Consider increasing your provisioning level with the UpdateTable API.

It's possible that ProvisionedThroughputExceededException is not actually the error. Try:

except botocore.exceptions.ClientError as pe:

instead.

If that doesn't work, figure out what line the error is occurring on and put the except statement there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM