-
Notifications
You must be signed in to change notification settings - Fork 434
Expose transaction cancellation reasons #1156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This will allow the caller to know e.g. which of the transaction items ran into a conflict, which can sometimes allow app logic to better handle the error without needing to make more requests.
| CancellationReason( | ||
| code=d['Code'], | ||
| message=d.get('Message'), | ||
| ) if d['Code'] != 'None' else None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why return None here? Shouldn't we instead not return anything at all?
The following code will break with AttributeError: 'NoneType' object has no attribute 'code':
except pydb_exc.TransactWriteError as e:
retry_err_types = (
'ProvisionedThroughputExceededException',
'ThrottlingError',
'TransactionConflict',
)
for error in e.cancellation_reasons:
if error.code in retry_err_types:
raise RuntimeError(f'transaction failed {error.code}')
Just seems like it'd be cleaner to not return anything vs returning None
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cancellation reasons are 1:1 to the transaction items so you can know what item was the reason.
Would you rather the reason itself not to be none but for code to be none?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah to me that would feel cleaner but I guess it's subjective. Just negates the extra "if error and error.code"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing is, if it was "cleaner", it would leave users with no way of associating reasons with individual transaction items. Maybe a different interface would convey this better, e.g. mapping of item to reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah that would make sense to me
This will allow the caller to know e.g. which of the transaction items ran into a conflict, which can sometimes allow app logic to better handle the error without needing to make more requests.
This is a backport of #1144 to 5.x branch (adjusted to Python 3.6 compatibility, hence no dataclass).
Fixes #891.