Skip to content

Commit

Permalink
Add new API for error reporting (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicoretti authored Aug 7, 2023
1 parent 5e6d9bc commit 9486161
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/changes/changes_0.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ T.B.D

### Refactoring

- #19: Rework error reporting API
- #17: Removed setup.py and updated poetry in actions
3 changes: 3 additions & 0 deletions exasol/error/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from exasol.error._error import ExaError, Parameter

__all__ = ["ExaError", "Parameter"]
95 changes: 95 additions & 0 deletions exasol/error/_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import warnings
from collections.abc import Iterable, Mapping
from dataclasses import dataclass
from typing import Dict, List, Union

with warnings.catch_warnings():
warnings.simplefilter("ignore")
from exasol_error_reporting_python import exa_error


@dataclass(frozen=True)
class Parameter:
value: str
description: Union[None, str]


class Error:
def __init__(
self,
name: str,
message: str,
mitigations: Union[str, Iterable[str]],
parameters: Dict[str, Union[str, Parameter]],
):
# This function maybe flattened into or moved out of the constructor in the future.
def build_error(code, msg, mitigations, params):
builder = exa_error.ExaError.message_builder(code)
builder.message(msg)

for mitigation in mitigations:
builder.mitigation(mitigation)

for k, v in params.items():
name, value, description = k, v, ""
if isinstance(v, Parameter):
value = v.value
description = v.description
builder.parameter(name, value, description)

return builder

self._error = build_error(name, message, mitigations, parameters)

def __str__(self) -> str:
return f"{self._error}"

# TODO: Implement __format__ to conveniently support long and short reporting format


def ExaError(
name: str,
message: str,
mitigations: Union[str, List[str]],
parameters: Mapping[str, Union[str, Parameter]],
) -> Error:
"""Create a new ExaError.
The caller is responsible to make sure that the chosen name/id is unique.
Args:
name: unique name/id of the error.
message: the error message itself. It can contain placeholders for parameters.
mitigations: One or multiple mitigations strings. A mitigation can contain placeholders for parameters.
parameters: which will be used for substitute the parameters in the mitigation(s) and the error message.
Returns:
An error type containing all relevant information.
Raises:
This function should not raise under any circumstances.
In case of error, it should report it also as an error type which is returned by this function.
Still the returned error should contain a references or information about the original call.
Attention:
FIXME: Due to legacy reasons this function currently still may raise an `ValueError` (Refactoring Required).
Potential error scenarios which should taken into account are the following ones:
* E-ERP-1: Invalid error code provided
params:
* Original ErrorCode
* Original error message
* Original mitigations
* Original parameters
* E-ERP-2 Unknown error/exception occurred
params:
* exception/msg
* Original ErrorCode
* Original error message
* Original mitigations
* Original parameters
"""
return Error(name, message, mitigations, parameters)
5 changes: 5 additions & 0 deletions exasol/error/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MAJOR = 0
MINOR = 4
PATCH = 0

VERSION = f"{MAJOR}.{MINOR}.{PATCH}"
18 changes: 17 additions & 1 deletion exasol_error_reporting_python/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
__version__ = '0.2.0'
import warnings


class ErrorReportingDeprecationWarning(DeprecationWarning):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

pass


warnings.warn(
f"The package {__name__} is deprecated, please use the 'exasol.error' package instead.",
ErrorReportingDeprecationWarning,
stacklevel=2,
)

__version__ = "0.4.0"
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[tool.poetry]
name = "exasol-error-reporting-python"
packages = [
{include = "exasol"},
{include = "exasol_error_reporting_python"}
]
version = "0.4.0"
description = "Exasol Python Error Reporting"
license = "MIT"
Expand Down
99 changes: 99 additions & 0 deletions test/unit/error_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import importlib
from collections import namedtuple
from contextlib import contextmanager
from inspect import cleandoc

import pytest

from exasol.error import ExaError, Parameter

Data = namedtuple("Data", ["code", "message", "mitigations", "parameters"])


@contextmanager
def does_not_raise():
try:
yield
except Exception as ex:
pytest.fail(f"Exception was raised where none is to be expected, details: {ex}")


@pytest.mark.parametrize(
"expected,data",
[
(
cleandoc(
"""
E-TEST-1: Not enough space on device '/dev/sda1'. Known mitigations:
* Delete something from '/dev/sda1'.
* Create larger partition.
"""
),
Data(
code="E-TEST-1",
message="Not enough space on device {{device}}.",
mitigations=[
"Delete something from {{device}}.",
"Create larger partition.",
],
parameters={"device": Parameter("/dev/sda1", "name of the device")},
),
),
(
cleandoc(
"""
E-TEST-1: Not enough space on device '/dev/sda1'. Known mitigations:
* Delete something from '/dev/sda1'.
* Create larger partition.
"""
),
Data(
code="E-TEST-1",
message="Not enough space on device {{device}}.",
mitigations=[
"Delete something from {{device}}.",
"Create larger partition.",
],
parameters={"device": "/dev/sda1"},
),
),
],
)
def test_exa_error_as_string(expected, data):
error = ExaError(data.code, data.message, data.mitigations, data.parameters)
actual = str(error)
assert actual == expected


@pytest.mark.xfail(
True,
reason="Old implementation does not avoid throwing exceptions, further refactoring is needed.",
)
@pytest.mark.parametrize(
"data",
[
(
Data(
code="BROKEN_ERROR_CODE",
message='"Not enough space on device {{device}}."',
mitigations=[
"Delete something from {{device}}.",
"Create larger partition.",
],
parameters={"device": Parameter("/dev/sda1", "name of the device")},
)
),
],
)
def test_exa_error_does_not_throw_error_on_invalid(data):
with does_not_raise():
_ = ExaError(data.code, data.message, data.mitigations, data.parameters)


def test_using_the_old_api_produces_deprecation_warning():
with pytest.warns(DeprecationWarning):
# due to the fact that the exasol.error package already imports the package at an earlier stage
# we need to ensure the module is imported during the test itself.
import exasol_error_reporting_python

importlib.reload(exasol_error_reporting_python)

0 comments on commit 9486161

Please sign in to comment.