Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/configcore/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ConfigError(Exception):
"""Base class for package exceptions."""


class UnknownLogLevelError(ConfigError):
"""Raised when a specified log level doesn't match any known log level."""

def __init__(self, invalid_value: str, valid_values: list) -> None:
"""Raise the error with correct message."""
super().__init__(
f"Invalid log level '{invalid_value}'. Must be one of: {valid_values}",
)
8 changes: 4 additions & 4 deletions src/configcore/loglevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from enum import IntEnum
from typing import Self

from .errors import UnknownLogLevelError


class LogLevel(IntEnum):
"""Represents the different log levels."""
Expand All @@ -23,12 +25,10 @@ def from_str(cls, value: str) -> Self:
LogLevel enum value.

Raises:
ValueError: If the string doesn't match any log level.
UnknownLogLevelError: If the string doesn't match any log level.
"""
try:
return cls[value.upper()]
except KeyError as e:
valid_values = [e.name for e in cls]
raise ValueError(
f"Invalid log level '{value}'. Must be one of: {valid_values}",
) from e
raise UnknownLogLevelError(value, valid_values) from e
10 changes: 6 additions & 4 deletions tests/configcore/settings_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os

import pytest
from pydantic import ValidationError
from pydantic_settings import SettingsConfigDict

from src.configcore import Environment, LogLevel, Settings
from src.configcore.errors import UnknownLogLevelError


class TestSettings:
Expand Down Expand Up @@ -56,10 +56,12 @@ def test_invalid_log_level(self) -> None:
"""Test that invalid log level raises error."""
os.environ["LOG_LEVEL"] = "invalid"

with pytest.raises(ValidationError) as exc_info:
with pytest.raises(UnknownLogLevelError) as exc_info:
Settings()
assert "log_level" in str(exc_info.value)
assert len(exc_info.value.errors()) == 1
assert str(exc_info.value) == (
"Invalid log level 'invalid'. "
"Must be one of: ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']"
)

def test_is_env_production(self) -> None:
"""Test is_env_production helper method."""
Expand Down