-
Notifications
You must be signed in to change notification settings - Fork 1
Add new API for error reporting #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c78d135
Add new API for error reporting
Nicoretti 3235305
Add additional test case
Nicoretti c0111ec
Fix type annotations in ExaError signature
Nicoretti 1a6d293
Refactor parameter argument format
Nicoretti 4596985
Added type annotations for Error constructor
Nicoretti df9d6f8
Require a list as argument for the mitigations
Nicoretti d482231
Addjust type hints to enforce a more strict api
Nicoretti 2466c61
Fix typos
Nicoretti f0f80d8
Update changelog
Nicoretti a79c827
Apply review feedback
Nicoretti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,4 +8,5 @@ T.B.D | |
|
|
||
| ### Refactoring | ||
|
|
||
| - #19: Rework error reporting API | ||
| - #17: Removed setup.py and updated poetry in actions | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from exasol.error._error import ExaError, Parameter | ||
|
|
||
| __all__ = ["ExaError", "Parameter"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): | ||
tkilias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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]], | ||
Nicoretti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) -> 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(): | ||
Nicoretti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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", | ||
tkilias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [ | ||
| ( | ||
| cleandoc( | ||
| """ | ||
| E-TEST-1: Not enough space on device '/dev/sda1'. Known mitigations: | ||
| * Delete something from '/dev/sda1'. | ||
| * Create larger partition. | ||
| """ | ||
| ), | ||
| Data( | ||
tkilias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.