File size: 1,165 Bytes
71e71e8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | from resources import phrases
from src.model.error.ErrorSource import ErrorSource
class Error:
"""
Error class
"""
def __init__(self, code, message, source: ErrorSource, only_message: bool = False):
"""
:param message: Either a phrases.py constant name (str) to be resolved lazily in the
currently active language, or an already resolved message text
"""
self.code = code
self.message = message
self.source = source
self.only_message = only_message
def get_message(self) -> str:
"""
Resolve the message, translating it if it's a phrases.py constant name
:return: The resolved message
"""
if isinstance(self.message, str) and hasattr(phrases, self.message):
return getattr(phrases, self.message)
return self.message
def __str__(self):
if self.only_message:
return self.get_message()
result = f"Error " + self.source + str(self.code) + ": " + self.get_message()
result += phrases.FORWARD_TO_SUPPORT_GROUP
return result
def build(self):
return str(self)
|