Skip to content
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

Open
wants to merge 5 commits into
base: 14.0
Choose a base branch
from

Conversation

TDu
Copy link
Member

@TDu TDu commented Oct 17, 2023

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

@OCA-git-bot
Copy link
Contributor

Hi @guewen, @sebalix, @simahawk,
some modules you are maintaining are being modified, check this out!

Comment on lines 44 to 59
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
Copy link
Member

@mmequignon mmequignon Oct 27, 2023

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:

Suggested change
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

Copy link
Member Author

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

@jbaudoux
Copy link
Contributor

@bguillot As discussed on OCA days, here is the PR proposal for catching errors in shopfloor

@jbaudoux
Copy link
Contributor

@bguillot Can you drop a review as you were the first to suggest such improvement?

Copy link

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.
If you want this PR to never become stale, please ask a PSC member to apply the "no stale" label.

@github-actions github-actions bot added the stale PR/Issue without recent activity, it'll be soon closed automatically. label Mar 24, 2024
@jbaudoux jbaudoux added no stale Use this label to prevent the automated stale action from closing this PR/Issue. and removed stale PR/Issue without recent activity, it'll be soon closed automatically. labels Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no stale Use this label to prevent the automated stale action from closing this PR/Issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants