refactor error handler
This commit is contained in:
parent
c6739ef28c
commit
0c316aa05b
5
src/delta_barth/_management.py
Normal file
5
src/delta_barth/_management.py
Normal file
@ -0,0 +1,5 @@
|
||||
from typing import Final
|
||||
|
||||
from delta_barth.errors import ErrorHandler
|
||||
|
||||
ERROR_HANDLER: Final[ErrorHandler] = ErrorHandler()
|
||||
@ -3,7 +3,7 @@ from __future__ import annotations
|
||||
from typing import TYPE_CHECKING, Final
|
||||
|
||||
from delta_barth.constants import DEFAULT_INTERNAL_ERR_CODE
|
||||
from delta_barth.types import DataPipelineErrors, doptResponseError
|
||||
from delta_barth.types import DataPipelineErrors, doptResponse
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from delta_barth.types import ErrorDescription
|
||||
@ -33,7 +33,7 @@ DATA_PIPELINE_ERRORS_DESCR: Final[tuple[ErrorDescription, ...]] = (
|
||||
)
|
||||
|
||||
|
||||
class ErrorManager:
|
||||
class ErrorHandler:
|
||||
def __init__(self) -> None:
|
||||
self._data_pipelines: DataPipelineErrors | None = None
|
||||
self._parse_data_pipeline_errors()
|
||||
@ -48,9 +48,9 @@ class ErrorManager:
|
||||
def _parse_data_pipeline_errors(self) -> None:
|
||||
if self._data_pipelines is not None:
|
||||
return
|
||||
parsed_errors: dict[str, doptResponseError] = {}
|
||||
parsed_errors: dict[str, doptResponse] = {}
|
||||
for err in DATA_PIPELINE_ERRORS_DESCR:
|
||||
parsed_errors[err[0]] = doptResponseError(status_code=err[1], description=err[2])
|
||||
parsed_errors[err[0]] = doptResponse(status_code=err[1], description=err[2])
|
||||
|
||||
self._data_pipelines = DataPipelineErrors(**parsed_errors)
|
||||
|
||||
@ -59,8 +59,8 @@ class ErrorManager:
|
||||
description: str,
|
||||
message: str = "",
|
||||
err_code: int = DEFAULT_INTERNAL_ERR_CODE,
|
||||
) -> doptResponseError:
|
||||
return doptResponseError(
|
||||
) -> doptResponse:
|
||||
return doptResponse(
|
||||
status_code=err_code,
|
||||
description=description,
|
||||
message=message,
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
import warnings
|
||||
from dataclasses import dataclass, field
|
||||
@ -10,7 +12,7 @@ from pydantic import BaseModel, SkipValidation
|
||||
ErrorDescription: TypeAlias = tuple[str, int, str]
|
||||
|
||||
|
||||
class doptResponseError(BaseModel):
|
||||
class doptResponse(BaseModel):
|
||||
status_code: SkipValidation[int]
|
||||
description: SkipValidation[str]
|
||||
message: SkipValidation[str] = ""
|
||||
@ -18,9 +20,9 @@ class doptResponseError(BaseModel):
|
||||
|
||||
@dataclass(slots=True)
|
||||
class DataPipelineErrors:
|
||||
SUCCESS: doptResponseError
|
||||
TOO_FEW_POINTS: doptResponseError
|
||||
BAD_QUALITY: doptResponseError
|
||||
SUCCESS: doptResponse
|
||||
TOO_FEW_POINTS: doptResponse
|
||||
BAD_QUALITY: doptResponse
|
||||
|
||||
|
||||
class HttpRequestTypes(enum.StrEnum):
|
||||
|
||||
@ -1,39 +1,38 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import asdict
|
||||
from typing import Any, cast
|
||||
|
||||
import pytest
|
||||
from typing import cast
|
||||
|
||||
import delta_barth._management
|
||||
from delta_barth import errors
|
||||
from delta_barth.types import doptResponseError
|
||||
from delta_barth.types import doptResponse
|
||||
|
||||
|
||||
def test_error_manager_parsing():
|
||||
def test_error_handler_parsing():
|
||||
predef_errs = errors.DATA_PIPELINE_ERRORS_DESCR
|
||||
|
||||
err_mgr = errors.ErrorManager()
|
||||
assert err_mgr.data_pipelines is not None
|
||||
parsed_pipe_errs = err_mgr.data_pipelines
|
||||
err_hdlr = delta_barth._management.ErrorHandler()
|
||||
assert err_hdlr.data_pipelines is not None
|
||||
parsed_pipe_errs = err_hdlr.data_pipelines
|
||||
parsed_pipe_errs = asdict(parsed_pipe_errs)
|
||||
|
||||
for err in predef_errs:
|
||||
dopt_err = cast(doptResponseError, parsed_pipe_errs[err[0]])
|
||||
assert isinstance(dopt_err, doptResponseError)
|
||||
dopt_err = cast(doptResponse, parsed_pipe_errs[err[0]])
|
||||
assert isinstance(dopt_err, doptResponse)
|
||||
assert dopt_err.status_code == err[1]
|
||||
assert dopt_err.description == err[2]
|
||||
assert dopt_err.message == ""
|
||||
|
||||
err_mgr._parse_data_pipeline_errors()
|
||||
err_hdlr._parse_data_pipeline_errors()
|
||||
|
||||
|
||||
def test_error_manager_internal():
|
||||
def test_error_handler_internal():
|
||||
DESCRIPTION = "test case"
|
||||
MESSAGE = "an error occurred"
|
||||
ERR_CODE = 101
|
||||
|
||||
err_mgr = errors.ErrorManager()
|
||||
new_err = err_mgr.internal_error(
|
||||
err_hdlr = delta_barth._management.ErrorHandler()
|
||||
new_err = err_hdlr.internal_error(
|
||||
description=DESCRIPTION,
|
||||
message=MESSAGE,
|
||||
err_code=ERR_CODE,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user