"""Error types for filesystem operations"""
from typing import Literal, Optional
FsErrorCode = Literal[
"ENOENT",
"EEXIST",
"EISDIR",
"ENOTDIR",
"ENOTEMPTY",
"EPERM",
"EINVAL",
"ENOSYS",
]
FsSyscall = Literal[
"open",
"stat",
"mkdir",
"rmdir",
"rm",
"unlink",
"rename",
"scandir",
"copyfile",
"access",
]
class ErrnoException(Exception):
"""Exception with errno-style attributes
Args:
code: POSIX error code (e.g., 'ENOENT')
syscall: System call name (e.g., 'open')
path: Optional path involved in the error
message: Optional custom message (defaults to code)
Example:
>>> raise ErrnoException('ENOENT', 'open', '/missing.txt')
ErrnoException: ENOENT: no such file or directory, open '/missing.txt'
"""
def __init__(
self,
code: FsErrorCode,
syscall: FsSyscall,
path: Optional[str] = None,
message: Optional[str] = None,
):
base = message if message else code
suffix = f" '{path}'" if path is not None else ""
error_message = f"{code}: {base}, {syscall}{suffix}"
super().__init__(error_message)
self.code = code
self.syscall = syscall
self.path = path