Skip to content

Commit

Permalink
refactor: straiten error logging
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <[email protected]>
  • Loading branch information
jkowalleck committed Jan 7, 2024
1 parent d05c2c6 commit 6b52845
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
27 changes: 15 additions & 12 deletions serializable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,22 @@
)
from xml.etree.ElementTree import Element, SubElement

from defusedxml import ElementTree as SafeElementTree # type: ignore
from defusedxml import ElementTree as SafeElementTree # type:ignore[import-untyped]

from ._logging import _logger
from .formatters import BaseNameFormatter, CurrentFormatter
from .helpers import BaseHelper

if TYPE_CHECKING: # pragma: no cover
from logging import getLogger
from typing import Literal, Protocol

# help `flake8-logging` -- https://github.com/adamchainz/flake8-logging/issues/84
_logger = getLogger(__name__)
else:
from abc import ABC

from ._logging import _logger

Protocol = ABC

# `Intersection` is still not implemented, so it is interim replaced by Union for any support
Expand Down Expand Up @@ -341,11 +346,10 @@ def from_json(cls: Type[_T], data: Dict[str, Any]) -> Optional[_T]:
v = str(v)
_data[k] = prop_info.concrete_type(v)
except AttributeError as e:
_logger.error('There was an AttributeError deserializing JSON to %s.\n'
'The Property is: %s\n'
'The Value was: %s\n'
'Exception: %s\n',
cls, prop_info, v, e)
_logger.exception('There was an AttributeError deserializing JSON to %s.\n'
'The Property is: %s\n'
'The Value was: %s\n',
cls, prop_info, v)
raise AttributeError(
f'There was an AttributeError deserializing JSON to {cls} the Property {prop_info}: {e}'
) from e
Expand Down Expand Up @@ -638,11 +642,10 @@ def strip_default_namespace(s: str) -> str:
else:
_data[decoded_k] = prop_info.concrete_type(child_e.text)
except AttributeError as e:
_logger.error('There was an AttributeError deserializing JSON to %s.\n'
'The Property is: %s\n'
'The Value was: %s\n'
'Exception: %s\n',
cls, prop_info, v, e)
_logger.exception('There was an AttributeError deserializing JSON to %s.\n'
'The Property is: %s\n'
'The Value was: %s\n',
cls, prop_info, v)
raise AttributeError(
f'There was an AttributeError deserializing XML to {cls} the Property {prop_info}: {e}'
) from e
Expand Down
8 changes: 6 additions & 2 deletions serializable/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
from datetime import date, datetime
from typing import TYPE_CHECKING, Any, Optional, Type, TypeVar, Union

from ._logging import _logger

if TYPE_CHECKING: # pragma: no cover
from logging import getLogger
from xml.etree.ElementTree import Element

from . import ObjectMetadataLibrary, ViewType

# help `flake8-logging` -- https://github.com/adamchainz/flake8-logging/issues/84
_logger = getLogger(__name__)
else:
from ._logging import _logger

_T = TypeVar('_T')


Expand Down

0 comments on commit 6b52845

Please sign in to comment.