Debian Codegraph @

mediafile/exceptions.py

raw
1 """Custom exceptions for MediaFile metadata handling."""
2
3 from __future__ import annotations
4
5
6 class MediaFileError(Exception):
7 """Base exception for all MediaFile-related errors."""
8
9 def __init__(
10 self,
11 message: str,
12 filename: str | None = None,
13 ):
14 self.filename = filename
15 self.message = message
16 super().__init__(self._format_message())
17
18 def _format_message(self) -> str:
19 if self.filename:
20 return f"{self.filename}: {self.message}"
21 return self.message
22
23 def __str__(self) -> str:
24 return self._format_message()
25
26
27 class UnreadableFileError(MediaFileError):
28 """Raised when Mutagen cannot extract information from the file."""
29
30 def __init__(
31 self,
32 filename: str,
33 message: str,
34 ):
35 super().__init__(
36 message,
37 filename,
38 )
39
40
41 class FileTypeError(UnreadableFileError):
42 """Reading this type of file is not supported.
43
44 If passed the `mutagen_type` argument this indicates that the
45 mutagen type is not supported by `Mediafile`.
46 """
47
48 def __init__(
49 self,
50 filename: str,
51 mutagen_type: str | None = None,
52 ):
53 if mutagen_type is None:
54 msg = "File is not in a recognized format"
55 else:
56 msg = f"File type '{mutagen_type}' is not supported"
57
58 super().__init__(filename, msg)
59
60
61 class MutagenError(UnreadableFileError):
62 """Raised when Mutagen fails unexpectedly, likely due to a bug."""
63
64 mutagen_exception: Exception
65
66 def __init__(self, filename: str, mutagen_exception: Exception):
67 self.mutagen_exception = mutagen_exception
68 message = f"Mutagen internal error: {mutagen_exception}"
69 super().__init__(filename, message)
70
71
72 __all__ = ["UnreadableFileError", "FileTypeError", "MutagenError"]