-
-
Notifications
You must be signed in to change notification settings - Fork 193
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
[14.0][IMP] shopfloor: add decorator to gracefully handle exceptions on response #768
base: 14.0
Are you sure you want to change the base?
Conversation
Finally it only contains tests so could be rename or better removed.
shopfloor_base/utils.py
Outdated
def _catch_errors(fn): | ||
@wraps(fn) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
res = fn(*args, **kwargs) | ||
except ValidationError as ex: | ||
message = { | ||
"message_type": "error", | ||
"body": ex.name, | ||
} | ||
return response_error(*args, **kwargs, message=message) | ||
return res | ||
|
||
return wrapper | ||
|
||
return _catch_errors |
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.
it would be nice to have a way to rollback the transaction in case of an error:
def _catch_errors(fn): | |
@wraps(fn) | |
def wrapper(*args, **kwargs): | |
try: | |
res = fn(*args, **kwargs) | |
except ValidationError as ex: | |
message = { | |
"message_type": "error", | |
"body": ex.name, | |
} | |
return response_error(*args, **kwargs, message=message) | |
return res | |
return wrapper | |
return _catch_errors | |
def _catch_errors(fn): | |
@wraps(fn) | |
def wrapper(self, *args, **kwargs, rollback=False): | |
savepoint = self._actions_for("savepoint").new() | |
try: | |
res = fn(self, *args, **kwargs) | |
except ValidationError as ex: | |
if rollback: | |
savepoint.rollback() | |
else: | |
savepoint.release() | |
message = { | |
"message_type": "error", | |
"body": ex.name, | |
} | |
return response_error(*args, **kwargs, message=message) | |
savepoint.release() | |
return res | |
return wrapper | |
return _catch_errors |
or smth like that.
In this implementation, I'm also checking that response isn't an error.
https://github.com/OCA/wms/blob/14.0/shopfloor_single_product_transfer/services/single_product_transfer.py#L20
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.
Thank you, yes it is needed, added a fixup
@@ -33,6 +34,37 @@ def wrapped(*args, **kwargs): | |||
return _ensure_model | |||
|
|||
|
|||
def catch_errors(response_error, exceptions_to_catch=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.
def catch_errors(response_error, exceptions_to_catch=None): | |
def with_savepoint(response_error, exceptions_to_catch=None): |
I would prefer this name, not blocking
@bguillot As discussed on OCA days, here is the PR proposal for catching errors in shopfloor |
@bguillot Can you drop a review as you were the first to suggest such improvement? |
There hasn't been any activity on this pull request in the past 4 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 30 days. |
This PR adds a decorator that can be used on any endpoint to catch some exceptions and return to the user a clean error message and not a 500 error message.
This is to solve the problem discussed on #732