Commit e425c376 authored by BorjaEst's avatar BorjaEst
Browse files

Add default return value to error log decorator

parent 7e2909b8
...@@ -26,7 +26,8 @@ vmro3_standard_coordinates = [ ...@@ -26,7 +26,8 @@ vmro3_standard_coordinates = [
] ]
@utils.return_on_failure("Error when loading '{0}'".format(tco3_standard_name)) @utils.return_on_failure("Error when loading '{0}'".format(tco3_standard_name),
xr.Dataset())
def __load_tco3(name, paths, coordinates): def __load_tco3(name, paths, coordinates):
"""Loads and standarises the tco3 data""" """Loads and standarises the tco3 data"""
logger.debug("Standard loading of '{0}' data".format(tco3_standard_name)) logger.debug("Standard loading of '{0}' data".format(tco3_standard_name))
...@@ -38,7 +39,8 @@ def __load_tco3(name, paths, coordinates): ...@@ -38,7 +39,8 @@ def __load_tco3(name, paths, coordinates):
return dataset.mean(dim=tco3_mean_coordinate) return dataset.mean(dim=tco3_mean_coordinate)
@utils.return_on_failure("Error when loading '{0}'".format(vmro3_standard_name)) @utils.return_on_failure("Error when loading '{0}'".format(vmro3_standard_name),
xr.Dataset())
def __load_vmro3(name, paths, coordinates): def __load_vmro3(name, paths, coordinates):
"""Loads and standarises the vmro3 data""" """Loads and standarises the vmro3 data"""
logger.debug("Standard loading of '{0}' data".format(vmro3_standard_name)) logger.debug("Standard loading of '{0}' data".format(vmro3_standard_name))
......
...@@ -35,12 +35,15 @@ def cd(newdir): ...@@ -35,12 +35,15 @@ def cd(newdir):
logger.debug("Restore directory: '%s'", prevdir) logger.debug("Restore directory: '%s'", prevdir)
def return_on_failure(message): def return_on_failure(message, default=None):
"""Decorator to do not break but log. Note that the error stack """Decorator to do not break but log. Note that the error stack
is printed as well to do not lose relevant information. is printed as well to do not lose relevant information.
:param message: Additional message to log when the function fails. :param message: Additional message to log when the function fails.
:type message: str :type message: str
:param default: Value to return if fails.
:type default: any or None, optional
""" """
def decorate(function): def decorate(function):
def applicator(*args, **kwargs): def applicator(*args, **kwargs):
...@@ -49,6 +52,7 @@ def return_on_failure(message): ...@@ -49,6 +52,7 @@ def return_on_failure(message):
except: except:
# Log error with stack using root (not utils) # Log error with stack using root (not utils)
logging.error(message, exc_info=True) logging.error(message, exc_info=True)
return default
return applicator return applicator
return decorate return decorate
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment