tests for new api errors

This commit is contained in:
Florian Förster 2025-03-05 15:05:23 +01:00
parent c375d8e9d4
commit e60cc99583
2 changed files with 56 additions and 15 deletions

View File

@ -6,7 +6,7 @@ from delta_barth.constants import DEFAULT_API_ERR_CODE, DEFAULT_INTERNAL_ERR_COD
from delta_barth.types import DataPipeStates, Status from delta_barth.types import DataPipeStates, Status
if TYPE_CHECKING: if TYPE_CHECKING:
from delta_barth.types import DelBarApiError, ErrorDescription from delta_barth.types import DelBarApiError, StatusDescription
class UnspecifiedRequestType(Exception): class UnspecifiedRequestType(Exception):
@ -26,14 +26,14 @@ class FeaturesMissingError(Exception):
## ** internal error handling ## ** internal error handling
DATA_PIPELINE_STATUS_DESCR: Final[tuple[ErrorDescription, ...]] = ( DATA_PIPELINE_STATUS_DESCR: Final[tuple[StatusDescription, ...]] = (
("SUCCESS", 0, "Erfolg"), ("SUCCESS", 0, "Erfolg"),
("TOO_FEW_POINTS", 1, "Datensatz besitzt nicht genügend Datenpunkte"), ("TOO_FEW_POINTS", 1, "Datensatz besitzt nicht genügend Datenpunkte"),
("BAD_QUALITY", 2, "Prognosequalität des Modells unzureichend"), ("BAD_QUALITY", 2, "Prognosequalität des Modells unzureichend"),
) )
class ErrorHandler: class StateHandler:
def __init__(self) -> None: def __init__(self) -> None:
self._pipe_states: DataPipeStates | None = None self._pipe_states: DataPipeStates | None = None
self._parse_data_pipe_states() self._parse_data_pipe_states()
@ -50,7 +50,7 @@ class ErrorHandler:
return return
parsed_errors: dict[str, Status] = {} parsed_errors: dict[str, Status] = {}
for err in DATA_PIPELINE_STATUS_DESCR: for err in DATA_PIPELINE_STATUS_DESCR:
parsed_errors[err[0]] = Status(status_code=err[1], description=err[2]) parsed_errors[err[0]] = Status(code=err[1], description=err[2])
self._pipe_states = DataPipeStates(**parsed_errors) self._pipe_states = DataPipeStates(**parsed_errors)
@ -58,10 +58,18 @@ class ErrorHandler:
self, self,
description: str, description: str,
message: str = "", message: str = "",
err_code: int = DEFAULT_INTERNAL_ERR_CODE, code: int = DEFAULT_INTERNAL_ERR_CODE,
) -> Status: ) -> Status:
lower_bound = DEFAULT_INTERNAL_ERR_CODE
upper_bound = DEFAULT_API_ERR_CODE
if code < lower_bound or code > upper_bound:
raise ValueError(
f"Custom error codes mus be between default "
f"values of {lower_bound}-{upper_bound}"
)
return Status( return Status(
status_code=err_code, code=code,
description=description, description=description,
message=message, message=message,
) )
@ -75,7 +83,7 @@ class ErrorHandler:
"Bitte beachten Sie die zusätzliche Fehlerausgabe des Servers in dieser Antwort" "Bitte beachten Sie die zusätzliche Fehlerausgabe des Servers in dieser Antwort"
) )
return Status( return Status(
status_code=DEFAULT_API_ERR_CODE, code=DEFAULT_API_ERR_CODE,
description=description, description=description,
message=message, message=message,
api_server_error=error, api_server_error=error,

View File

@ -3,15 +3,18 @@ from __future__ import annotations
from dataclasses import asdict from dataclasses import asdict
from typing import cast from typing import cast
import pytest
import delta_barth._management import delta_barth._management
from delta_barth import errors from delta_barth import errors
from delta_barth.types import Status from delta_barth.constants import DEFAULT_API_ERR_CODE
from delta_barth.types import DelBarApiError, Status
def test_error_handler_parsing(): def test_state_handler_parsing():
predef_errs = errors.DATA_PIPELINE_STATUS_DESCR predef_errs = errors.DATA_PIPELINE_STATUS_DESCR
err_hdlr = delta_barth._management.ErrorHandler() err_hdlr = delta_barth._management.StateHandler()
assert err_hdlr.pipe_states is not None assert err_hdlr.pipe_states is not None
parsed_pipe_errs = err_hdlr.pipe_states parsed_pipe_errs = err_hdlr.pipe_states
parsed_pipe_errs = asdict(parsed_pipe_errs) parsed_pipe_errs = asdict(parsed_pipe_errs)
@ -19,24 +22,54 @@ def test_error_handler_parsing():
for err in predef_errs: for err in predef_errs:
dopt_err = cast(Status, parsed_pipe_errs[err[0]]) dopt_err = cast(Status, parsed_pipe_errs[err[0]])
assert isinstance(dopt_err, Status) assert isinstance(dopt_err, Status)
assert dopt_err.status_code == err[1] assert dopt_err.code == err[1]
assert dopt_err.description == err[2] assert dopt_err.description == err[2]
assert dopt_err.message == "" assert dopt_err.message == ""
err_hdlr._parse_data_pipe_states() err_hdlr._parse_data_pipe_states()
def test_error_handler_internal(): def test_state_handler_internal():
DESCRIPTION = "test case" DESCRIPTION = "test case"
MESSAGE = "an error occurred" MESSAGE = "an error occurred"
ERR_CODE = 101 ERR_CODE = 101
err_hdlr = delta_barth._management.ErrorHandler() err_hdlr = delta_barth._management.StateHandler()
new_err = err_hdlr.error( new_err = err_hdlr.error(
description=DESCRIPTION, description=DESCRIPTION,
message=MESSAGE, message=MESSAGE,
err_code=ERR_CODE, code=ERR_CODE,
) )
assert new_err.status_code == ERR_CODE assert new_err.code == ERR_CODE
assert new_err.description == DESCRIPTION assert new_err.description == DESCRIPTION
assert new_err.message == MESSAGE assert new_err.message == MESSAGE
# failure cases
err_code = 50 # default lower bound: 100
with pytest.raises(ValueError):
new_err = err_hdlr.error(
description=DESCRIPTION,
message=MESSAGE,
code=err_code,
)
err_code = 500 # default upper bound: 400
with pytest.raises(ValueError):
new_err = err_hdlr.error(
description=DESCRIPTION,
message=MESSAGE,
code=err_code,
)
def test_state_handler_api_error():
MESSAGE = "an error occurred"
api_err = DelBarApiError(status_code=401, message="test case")
assert api_err.status_code == 401
assert api_err.message == "test case"
err_hdlr = delta_barth._management.StateHandler()
new_err = err_hdlr.api_error(error=api_err)
assert new_err.code == DEFAULT_API_ERR_CODE
assert "API-Server" in new_err.description
assert new_err.message != MESSAGE
assert new_err.api_server_error is not None
assert new_err.api_server_error == api_err