Skip to content

Log input parameters of specific failing methods #40

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

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/zinolib/controllers/zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@
"""

from datetime import datetime, timezone
from typing import Iterable, List, TypedDict, Optional, Set
from typing import Dict, Iterable, List, TypedDict, Optional, Set
import logging

from .base import EventManager
from ..compat import StrEnum
from ..event_types import EventType, Event, HistoryEntry, LogEntry, AdmState
from ..event_types import PortStateEvent
from ..ritz import ZinoError, ritz, notifier
from ..utils import log_exception_with_params


__all__ = [
Expand Down Expand Up @@ -248,7 +249,8 @@ def get_attrlist(request, event_id: int):
return request.get_raw_attributes(event_id)

@classmethod
def attrlist_to_attrdict(cls, attrlist: Iterable[str]):
@log_exception_with_params(LOG)
def attrlist_to_attrdict(cls, attrlist: Iterable[str]) -> Dict[str, str]:
"""Translate a wire protocol dump of a single event

The dump is a list of lines of the format:
Expand Down Expand Up @@ -290,6 +292,7 @@ def get_history(request, event_id: int):
return request.get_raw_history(event_id).data

@classmethod
@log_exception_with_params(LOG)
def parse_response(cls, history_data: Iterable[str]) -> list[HistoryDict]:
"""
Input:
Expand Down Expand Up @@ -357,6 +360,7 @@ def get_log(request, event_id: int) -> list[str]:
return request.get_raw_log(event_id).data

@staticmethod
@log_exception_with_params(LOG)
def parse_response(log_data: Iterable[str]) -> list[LogDict]:
"""
Input:
Expand Down
25 changes: 25 additions & 0 deletions src/zinolib/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import functools
import hashlib
from typing import Any


__all__ = [
Expand Down Expand Up @@ -56,3 +58,26 @@ def generate_authtoken(challenge, password):
raw_token = "%s %s" % (challenge, password)
token = hashlib.sha1(raw_token.encode("UTF-8")).hexdigest()
return token


def log_exception_with_params(logger, reraise=True, return_value=None):
"""Log the params and exception if the wrapped function fails

If ``reraise`` is False, return ``return_value`` instead of reraising the
exception.
"""
def inner(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
try:
result = function(*args, **kwargs)
return result
except Exception as e:
params = f'args={args} kwargs={kwargs}'
funcname = function.__name__
logger.exception(f'"{funcname}" failed with: {params}\n{e}')
if reraise:
raise
return return_value
return wrapper
return inner