Skip to content

Commit

Permalink
Fix log
Browse files Browse the repository at this point in the history
  • Loading branch information
bart0003 committed Dec 14, 2023
1 parent 24c2cd2 commit c1ddfb8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
5 changes: 0 additions & 5 deletions core/message/from_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,6 @@ def message_name(self) -> str:
def message_name(self, message_name: str):
self._value[self.MESSAGE_NAME] = message_name

# unique message_id
@property
def incremental_id(self) -> int:
return self.as_dict[self.MESSAGE_ID]

@property
def as_dict(self) -> Dict[str, Any]:
return self._value
Expand Down
7 changes: 7 additions & 0 deletions core/message/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


class SmartAppMessage(ABC):
MESSAGE_ID = "messageId"

def __init__(self, validators: Iterable[BaseMessageValidator] = (), *args, **kwargs):
self.validators = validators

Expand All @@ -25,3 +27,8 @@ def payload(self):
@abstractmethod
def as_dict(self):
...

# unique message_id
@property
def incremental_id(self) -> int:
return self.as_dict[self.MESSAGE_ID]
18 changes: 15 additions & 3 deletions core/message/validators/base_validator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, TypeVar, Literal, Union
from core.logging.logger_utils import log
from core.logging.logger_utils import log, MESSAGE_ID_STR

if TYPE_CHECKING:
from core.message.message import SmartAppMessage
Expand All @@ -26,10 +26,22 @@ def validate(self, message: SmartAppMessage):
self._validate(message)
except self.VALIDATOR_EXCEPTION as e:
if self.on_exception == "log":
log(f"{self.__class__.__name__} Error: {e}")
else:
self._log(e, message)
if self.on_exception == "raise":
self._log(e, message, level="ERROR")
raise

def _log_params(self, message: SmartAppMessage):
return {
"key_name": "message_validator",
MESSAGE_ID_STR: message.incremental_id,
"validator": self.__class__.__name__,
}

def _log(self, exception: VALIDATOR_EXCEPTION, message: SmartAppMessage, level="WARNING"):
log_params = self._log_params(message)
log(f"{self.__class__.__name__} Error: {exception}", params=log_params, level=level)

@abstractmethod
def _validate(self, message: SmartAppMessage):
raise NotImplementedError
9 changes: 9 additions & 0 deletions smart_kit/message/validators/json_schema_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

import fastjsonschema
import jsonschema

from core.message.message import SmartAppMessage
from core.logging.logger_utils import log
from smart_kit.message.validators.base_validator_with_resources import BaseMessageValidatorWithResources

if TYPE_CHECKING:
Expand All @@ -23,6 +26,12 @@ def _compile_schema(schema: dict) -> Callable[[dict], None]:
instance = cls(schema)
return instance.validate

def _log(self, exception: VALIDATOR_EXCEPTION, message: SmartAppMessage, level="WARNING"):
log_params = self._log_params(message)
log_params["json_path"] = exception.json_path
log(f"{self.__class__.__name__} Error: {exception.message} on path: {exception.json_path}", params=log_params,
level=level)

def _validate(self, message: SmartAppFromMessage):
validator = self._schemas.get(message.message_name)
validator(message.payload)
Expand Down

0 comments on commit c1ddfb8

Please sign in to comment.