watchman/decorators.py
def check(func):
    """
    Decorator which wraps checks and returns an error response on failure.
    """
    def wrapped(*args, **kwargs):
        check_name = func.__name__
        arg_name = None
        if args:
            arg_name = args[0]
        try:
            if arg_name:
                logger.debug("Checking '%s' for '%s'", check_name, arg_name)
            else:
                logger.debug("Checking '%s'", check_name)
            response = func(*args, **kwargs)
        except Exception as e:
            message = str(e)
            response = {
                "ok": False,
                "error": message,
                "stacktrace": traceback.format_exc(),
            }
            # The check contains several individual checks (e.g., one per
            # database). Preface the results by name.
            if arg_name:
                response = {arg_name: response}
                logger.exception(
                    "Error calling '%s' for '%s': %s",
                    check_name,
                    arg_name,
                    message
                )
            else:
                logger.exception(
                    "Error calling '%s': %s",
                    check_name,
                    message
                )

        return response
    return wrapped
def check(func):
    """
    Decorator which wraps checks and returns an error response on failure.
    """
    def wrapped(*args, **kwargs):
        check_name = func.__name__
        arg_name = None
        if args:
            arg_name = args[0]
        try:
            if arg_name:
                logger.debug("Checking '%s' for '%s'", check_name, arg_name)
            else:
                logger.debug("Checking '%s'", check_name)
            response = func(*args, **kwargs)
        except Exception as e:
            message = str(e)
            response = {
                "ok": False,
                "error": message,
                "stacktrace": traceback.format_exc(),
            }
            # The check contains several individual checks (e.g., one per
            # database). Preface the results by name.
            if arg_name:
                response = {arg_name: response}
                logger.exception(
                    "Error calling '%s' for '%s': %s",
                    check_name,
                    arg_name,
                    message
                )
            else:
                logger.exception(
                    "Error calling '%s': %s",
                    check_name,
                    message
                )

        return response
    return wrapped
Decorator which wraps checks and returns an error response on failure.