Source code for darc.error
# -*- coding: utf-8 -*-
"""Custom Exceptions
=======================
The :func:`~darc.error.render_error` function can be used to render
multi-line error messages with :mod:`stem.util.term` colours.
The :mod:`darc` project provides following custom exceptions:
* :exc:`~darc.error.LinkNoReturn`
* :exc:`~darc.error.UnsupportedLink`
* :exc:`~darc.error.UnsupportedPlatform`
* :exc:`~darc.error.UnsupportedProxy`
* :exc:`~darc.error.WorkerBreak`
.. note::
All exceptions are inherited from :exc:`~darc.error._BaseException`.
The :mod:`darc` project provides following custom warnings:
* :exc:`~darc.error.TorBootstrapFailed`
* :exc:`~darc.error.I2PBootstrapFailed`
* :exc:`~darc.error.ZeroNetBootstrapFailed`
* :exc:`~darc.error.FreenetBootstrapFailed`
* :exc:`~darc.error.APIRequestFailed`
* :exc:`~darc.error.SiteNotFoundWarning`
* :exc:`~darc.error.LockWarning`
* :exc:`~darc.error.TorRenewFailed`
* :exc:`~darc.error.RedisCommandFailed`
* :exc:`~darc.error.HookExecutionFailed`
.. note::
All warnings are inherited from :exc:`~darc.error._BaseWarning`.
"""
from typing import TYPE_CHECKING
import stem.util.term
if TYPE_CHECKING:
from typing import AnyStr
from stem.util.term import Color
[docs]class _BaseException(Exception):
"""Base exception class for :mod:`darc` module."""
[docs]class LinkNoReturn(_BaseException):
"""The link has no return value from the hooks.
Args:
link (darc.link.Link): Original link object.
Keyword Args:
drop: If drops the ``link`` from task queues.
"""
[docs] def __init__(self, link=None, *, drop: bool = True) -> None: # type: ignore[no-untyped-def]
self.link = link
self.drop = drop
super().__init__()
[docs]class UnsupportedLink(_BaseException):
"""The link is not supported."""
[docs]class UnsupportedProxy(_BaseException):
"""The proxy is not supported."""
[docs]class WorkerBreak(_BaseException):
"""Break from the worker loop."""
[docs]class _BaseWarning(Warning):
"""Base warning for :mod:`darc` module."""
[docs]class TorBootstrapFailed(_BaseWarning):
"""Tor bootstrap process failed."""
[docs]class TorRenewFailed(_BaseWarning):
"""Tor renew request failed."""
[docs]class I2PBootstrapFailed(_BaseWarning):
"""I2P bootstrap process failed."""
[docs]class ZeroNetBootstrapFailed(_BaseWarning):
"""ZeroNet bootstrap process failed."""
[docs]class FreenetBootstrapFailed(_BaseWarning):
"""Freenet bootstrap process failed."""
[docs]class RedisCommandFailed(_BaseWarning):
"""Redis command execution failed."""
[docs]class DatabaseOperaionFailed(_BaseWarning):
"""Database operation execution failed."""
[docs]class APIRequestFailed(_BaseWarning):
"""API submit failed."""
[docs]class SiteNotFoundWarning(_BaseWarning, ImportWarning):
"""Site customisation not found."""
[docs]class LockWarning(_BaseWarning):
"""Failed to acquire Redis lock."""
[docs]class HookExecutionFailed(_BaseWarning):
"""Failed to execute hook function."""
[docs]def render_error(message: 'AnyStr', colour: 'Color') -> str:
"""Render error message.
The function wraps the :func:`stem.util.term.format` function to
provide multi-line formatting support.
Args:
message: Multi-line message to be rendered with ``colour``.
colour (stem.util.term.Color): Front colour of text, c.f.
:class:`stem.util.term.Color`.
Returns:
The rendered error message.
"""
return ''.join(
stem.util.term.format(line, colour) for line in message.splitlines(True)
)