From 041e7e537bb910cf9b70cb662b53ca2479d11ff7 Mon Sep 17 00:00:00 2001 From: Thierry Ducrest Date: Mon, 30 Oct 2023 13:14:15 +0100 Subject: [PATCH] fixup! fixup! sf_base: add decorator to gracefully handle exceptions --- shopfloor_base/utils.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/shopfloor_base/utils.py b/shopfloor_base/utils.py index a3e0aeb396..fbff6ae162 100644 --- a/shopfloor_base/utils.py +++ b/shopfloor_base/utils.py @@ -5,7 +5,7 @@ import os from functools import wraps -from odoo.exceptions import ValidationError +from odoo.exceptions import UserError, ValidationError from odoo.modules.module import load_information_from_description_file from odoo.tools.config import config as odoo_config @@ -34,24 +34,27 @@ def wrapped(*args, **kwargs): return _ensure_model -def catch_errors(response_error): +def catch_errors(response_error, exceptions_to_catch=None): """Decorator for service endpoints to gracefully handle some exceptions. The decorator is passed a function with the same arguments than the function handlling the endpoint, plus a keyword argument called `message`. """ + if exceptions_to_catch is None: + exceptions_to_catch = (ValidationError, UserError) + def _catch_errors(fn): @wraps(fn) def wrapper(self, *args, **kwargs): savepoint = self._actions_for("savepoint").new() try: res = fn(self, *args, **kwargs) - except ValidationError as ex: + except exceptions_to_catch as ex: savepoint.rollback() message = { "message_type": "error", - "body": ex.name, + "body": str(ex), } return response_error(self, *args, **kwargs, message=message) savepoint.release()