Skip to content

Commit

Permalink
implement __format__ for a more fine grained output of the error (#50)
Browse files Browse the repository at this point in the history
---------
Co-authored-by: Nicola Coretti <[email protected]>
  • Loading branch information
Jannis-Mittenzwei authored Jan 16, 2025
1 parent babe4a2 commit 9a7ceb0
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 5 deletions.
4 changes: 4 additions & 0 deletions doc/changes/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@

* Relocked Dependencies
* Update toolbox workflows

## Added

* Added support for finegrained error output control
41 changes: 39 additions & 2 deletions exasol/error/_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,45 @@ def build_error(code, msg, mitigations, params):
def __str__(self) -> str:
return f"{self._error}"

# TODO: Implement __format__ to conveniently support long and short reporting format
# See also see, Github Issue #28 https://github.com/exasol/error-reporting-python/issues/28
def __format__(self, format_spec: str):
def _name(error: Error):
return f"{error._error._error_code} "

def _message(error: Error):
return f"{error._error._message_builder} "

def _mitigations(error: Error):
return f"{error._error._mitigations} "

def _parameters(error: Error):
return f"{error._error._parameter_dict} "

def _short(error: Error):
return _name(error) + _message(error)

def _long(error: Error):
return (
_name(error)
+ _message(error)
+ _mitigations(error)
+ _parameters(error)
)

output = ""
formats = {
"name": _name(self),
"message": _message(self),
"mitigations": _mitigations(self),
"parameters": _parameters(self),
"short": _short(self),
"long": _long(self),
"": "",
}
for part in format_spec.split(" "):
if part in formats:
output += formats[part]

return output[:-1]


# ATTENTION: In the event of an exception while creating an error, we encounter a chicken-and-egg problem regarding error definitions.
Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

131 changes: 131 additions & 0 deletions test/unit/fmt_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import pytest

from exasol.error._error import Error


@pytest.mark.parametrize(
"error, fmt, expected",
[
(
Error(
code="E-TEST-1",
message="Not enough space on device {/dev/sda1}.",
mitigations=[
"Delete something from {/dev/sda1}",
"Create larger partition.",
],
parameters={"device": "/dev/sda1"},
),
"",
"",
),
(
Error(
code="E-TEST-1",
message="Not enough space on device {/dev/sda1}.",
mitigations=[
"Delete something from {/dev/sda1}",
"Create larger partition.",
],
parameters={"device": "/dev/sda1"},
),
"unknown",
"",
),
(
Error(
code="E-TEST-1",
message="Not enough space on device {/dev/sda1}.",
mitigations=[
"Delete something from {/dev/sda1}",
"Create larger partition.",
],
parameters={"device": "/dev/sda1"},
),
"long",
"E-TEST-1 ['Not enough space on device {/dev/sda1}.'] ['Delete something from {/dev/sda1}', 'Create larger partition.'] {'device': '/dev/sda1'}",
),
(
Error(
code="E-TEST-1",
message="Not enough space on device {/dev/sda1}.",
mitigations=[
"Delete something from {/dev/sda1}",
"Create larger partition.",
],
parameters={"device": "/dev/sda1"},
),
"short",
"E-TEST-1 ['Not enough space on device {/dev/sda1}.']",
),
(
Error(
code="E-TEST-1",
message="Not enough space on device {/dev/sda1}.",
mitigations=[
"Delete something from {/dev/sda1}",
"Create larger partition.",
],
parameters={"device": "/dev/sda1"},
),
"name",
"E-TEST-1",
),
(
Error(
code="E-TEST-1",
message="Not enough space on device {/dev/sda1}.",
mitigations=[
"Delete something from {/dev/sda1}",
"Create larger partition.",
],
parameters={"device": "/dev/sda1"},
),
"message",
"['Not enough space on device {/dev/sda1}.']",
),
(
Error(
code="E-TEST-1",
message="Not enough space on device {/dev/sda1}.",
mitigations=[
"Delete something from {/dev/sda1}",
"Create larger partition.",
],
parameters={"device": "/dev/sda1"},
),
"mitigations",
"['Delete something from {/dev/sda1}', 'Create larger partition.']",
),
(
Error(
code="E-TEST-1",
message="Not enough space on device {/dev/sda1}.",
mitigations=[
"Delete something from {/dev/sda1}",
"Create larger partition.",
],
parameters={"device": "/dev/sda1"},
),
"parameters",
"{'device': '/dev/sda1'}",
),
(
Error(
code="E-TEST-1",
message="Not enough space on device {/dev/sda1}.",
mitigations=[
"Delete something from {/dev/sda1}",
"Create larger partition.",
],
parameters={"device": "/dev/sda1"},
),
"parameters mitigations message name",
"{'device': '/dev/sda1'} ['Delete something from {/dev/sda1}', 'Create larger partition.'] ['Not enough space on device {/dev/sda1}.'] E-TEST-1",
),
],
)
def test_fmt(error, fmt, expected):
fmt_str = f"{{error:{fmt}}}"
actual = fmt_str.format(error=error)
assert actual == expected

0 comments on commit 9a7ceb0

Please sign in to comment.