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