🎨 Work on import error handling
- Generic ErrorProducer class - Importer and managers are error producers - SGDB Auth friendly error - Bad source location friendly errors (data, config, cache) - Removed unused decorators
This commit is contained in:
28
src/errors/error_producer.py
Normal file
28
src/errors/error_producer.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from threading import Lock
|
||||
|
||||
|
||||
class ErrorProducer:
|
||||
"""
|
||||
A mixin for objects that produce errors.
|
||||
|
||||
Specifies the report_error and collect_errors methods in a thread-safe manner.
|
||||
"""
|
||||
|
||||
errors: list[Exception] = None
|
||||
errors_lock: Lock = None
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.errors = []
|
||||
self.errors_lock = Lock()
|
||||
|
||||
def report_error(self, error: Exception):
|
||||
"""Report an error"""
|
||||
with self.errors_lock:
|
||||
self.errors.append(error)
|
||||
|
||||
def collect_errors(self) -> list[Exception]:
|
||||
"""Collect and remove the errors produced by the object"""
|
||||
with self.errors_lock:
|
||||
errors = self.errors.copy()
|
||||
self.errors.clear()
|
||||
return errors
|
||||
47
src/errors/friendly_error.py
Normal file
47
src/errors/friendly_error.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from typing import Iterable
|
||||
|
||||
|
||||
class FriendlyError(Exception):
|
||||
"""
|
||||
An error that is supposed to be shown to the user in a nice format
|
||||
|
||||
Use `raise ... from ...` to preserve context.
|
||||
"""
|
||||
|
||||
title_format: str
|
||||
title_args: Iterable[str]
|
||||
subtitle_format: str
|
||||
subtitle_args: Iterable[str]
|
||||
|
||||
@property
|
||||
def title(self) -> str:
|
||||
"""Get the gettext translated error title"""
|
||||
return _(self.title_format).format(self.title_args)
|
||||
|
||||
@property
|
||||
def subtitle(self) -> str:
|
||||
"""Get the gettext translated error subtitle"""
|
||||
return _(self.subtitle_format).format(self.subtitle_args)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
title: str,
|
||||
subtitle: str,
|
||||
title_args: Iterable[str] = None,
|
||||
subtitle_args: Iterable[str] = None,
|
||||
) -> None:
|
||||
"""Create a friendly error
|
||||
|
||||
:param str title: The error's title, translatable with gettext
|
||||
:param str subtitle: The error's subtitle, translatable with gettext
|
||||
"""
|
||||
super().__init__()
|
||||
if title is not None:
|
||||
self.title_format = title
|
||||
if subtitle is not None:
|
||||
self.subtitle_format = subtitle
|
||||
self.title_args = title_args if title_args else ()
|
||||
self.subtitle_args = subtitle_args if subtitle_args else ()
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.title} - {self.subtitle}"
|
||||
Reference in New Issue
Block a user