Skip to content

Commit

Permalink
exceptions can have a detailed message that is only used for leaf errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Nov 5, 2024
1 parent 43449b2 commit d1a06af
Show file tree
Hide file tree
Showing 4 changed files with 602 additions and 176 deletions.
15 changes: 13 additions & 2 deletions schema_salad/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ def __init__(
sl: Optional[SourceLine] = None,
children: Optional[Sequence["SchemaSaladException"]] = None,
bullet_for_children: str = "",
detailed_message: Optional[str] = None,
) -> None:
super().__init__(msg)
self.message = self.args[0]
self.detailed_message = detailed_message
self.file: Optional[str] = None
self.start: Optional[tuple[int, int]] = None
self.end: Optional[tuple[int, int]] = None
Expand Down Expand Up @@ -97,14 +99,23 @@ def summary(self, level: int = 0, with_bullet: bool = False) -> str:
indent_per_level = 2
spaces = (level * indent_per_level) * " "
bullet = self.bullet + " " if len(self.bullet) > 0 and with_bullet else ""
return f"{self.prefix()}{spaces}{bullet}{self.message}"
message_string = (
self.detailed_message
if (len(self.children) < 1 and self.detailed_message)
else self.message
)
return f"{self.prefix()}{spaces}{bullet}{message_string}"

def __str__(self) -> str:
"""Convert to a string using :py:meth:`pretty_str`."""
return str(self.pretty_str())

def pretty_str(self, level: int = 0) -> str:
messages = len(self.message)
messages = (
len(self.message)
if len(self.children) > 0
else len(self.detailed_message or self.message)
)
my_summary = [self.summary(level, True)] if messages else []
next_level = level + 1 if messages else level

Expand Down
Loading

0 comments on commit d1a06af

Please sign in to comment.