Coverage for lice2/api/exceptions.py: 100%

1"""Define custom exceptions for the API."""

2

3 from __future__ import annotations

4

5

6 class LiceError(Exception):

7 """Base class for all exceptions in the Lice API."""

8

9

10 class LicenseNotFoundError(LiceError):

11 """Raised when a license is not found in the database."""

12

13 def __init__(self, license_name: str) -> None:

14 """Initialize the LicenseNotFoundError exception.

15

16 Args:

17 license_name: The name of the license that was not found.

18 """

19 self.license_name = license_name

20 super().__init__(f"License '{self.license_name}' is unknown.")

21

22

23 class LanguageNotFoundError(LiceError):

24 """Raised when a language is not found in the database."""

25

26 def __init__(self, language_name: str) -> None:

27 """Initialize the LanguageNotFoundError exception.

28

29 Args:

30 language_name: The name of the language that was not found.

31 """

32 self.language_name = language_name

33 super().__init__(f"Language '{self.language_name}' is unknown.")

34

35

36 class HeaderNotFoundError(LiceError):

37 """Raised when a header is not found for the supplied license."""

38

39 def __init__(self, license_name: str) -> None:

40 """Initialize the NoHeaderFoundError exception.

41

42 Args:

43 license_name: The name of the license without a header.

44 """

45 self.license_name = license_name

46 super().__init__(

47 f"License '{self.license_name}' does not have any headers."

48 )

49

50

51 class InvalidYearError(LiceError):

52 """Raised when an invalid year is supplied."""

53

54 def __init__(self, year: str | int) -> None:

55 """Initialize the InvalidYearError exception.

56

57 Args:

58 year: The year that was not valid.

59 """

60 self.year = year

61 super().__init__(

62 f"Year '{self.year}' is not a valid year (must be 4 digits)."

63 )