Source code for ds_common_placeholder_py_lib.errors
"""
**File:** ``errors.py``
**Region:** ``ds-common-placeholder-py-lib``
Description
-----------
Typed errors. A message names the placeholder text and the JSON path, never a
resolved value, so it is safe to put in events and logs.
"""
from __future__ import annotations
from enum import StrEnum
[docs]
class ErrorCode(StrEnum):
"""Stable error codes. Callers map them to failure events."""
SYNTAX = "syntax"
UNKNOWN_NAMESPACE = "unknown_namespace"
UNKNOWN_FUNCTION = "unknown_function"
ARITY = "arity"
ARGUMENT = "argument"
MISSING_VALUE = "missing_value"
TYPE = "type"
SECRET_SCOPE = "secret_scope" # nosec B105
SECRET_MISSING = "secret_missing" # nosec B105
SECRET_UNAVAILABLE = "secret_unavailable" # nosec B105
SQL_POSITION = "sql_position"
SQL_SECRET = "sql_secret" # nosec B105
PROTECTED_FIELD = "protected_field"
CYCLE = "cycle"
_PLACEHOLDER_MAX = 64
[docs]
class PlaceholderError(Exception):
"""A placeholder could not be validated or resolved.
Args:
code: The error code.
message: What went wrong. Must not contain resolved values.
path: JSON Pointer of the string that holds the placeholder.
placeholder: The placeholder text as written in the definition. For
a ``syntax`` error it is input text that may be sensitive (an
unclosed ``@{`` runs to the end of the string): it is cut to 64
characters, and ``str()`` and ``repr()`` leave it out.
reference: The reference whose value caused the error, as written
(``owner.settings.region``; a whole namespace is its name), when
one reference is at fault: its value is missing, ``null`` or
blank, of the wrong type, or the namespace is not there. ``None``
when the error is not one reference's (a function's own check, a
vault read, every ``coalesce`` argument missing).
namespaces: The namespaces the part that failed reads: the
reference at fault, or the innermost call that failed with
everything nested in it, plus the namespace of *reference*.
Empty for an error found before anything is read (``syntax``,
``lookup``'s own arguments, a SQL position, a secret call in
SQL), and from ``validate``.
"""
def __init__(
self,
code: ErrorCode,
message: str,
*,
path: str = "",
placeholder: str | None = None,
reference: str | None = None,
namespaces: frozenset[str] = frozenset(),
) -> None:
super().__init__(message)
self.code = code
self.message = message
self.path = path
self.placeholder = placeholder
self.reference = reference
self.namespaces = namespaces
self._scoped = False # namespaces set for the part that failed
@property
def placeholder(self) -> str | None:
"""The placeholder text; at most 64 characters for ``syntax``."""
if self._placeholder is not None and self.code is ErrorCode.SYNTAX:
return self._placeholder[:_PLACEHOLDER_MAX]
return self._placeholder
@placeholder.setter
def placeholder(self, value: str | None) -> None:
self._placeholder = value
def __str__(self) -> str:
where = f" at {self.path!r}" if self.path else ""
shown = self.placeholder is not None and self.code is not ErrorCode.SYNTAX
text = f" in {self.placeholder!r}" if shown else ""
return f"{self.code.value}: {self.message}{where}{text}"
def __repr__(self) -> str:
shown = None if self.code is ErrorCode.SYNTAX else self.placeholder
return f"PlaceholderError(code={self.code.value!r}, path={self.path!r}, placeholder={shown!r})"
[docs]
class MissingValue(Exception):
"""Raised by a provider when a path has no value."""
class _Absent(MissingValue):
"""No value, with what to say about it: raised while a placeholder is
evaluated, so an enclosing ``coalesce`` can skip it, and reported as
``missing_value`` once it reaches the placeholder written in the text.
The message is the library's own, never a provider's text."""
message = "no value at this path"
def __init__(
self, message: str | None = None, *, reference: str | None = None, namespaces: frozenset[str] | None = None
) -> None:
super().__init__()
if message is not None:
self.message = message
self.reference = reference
self.namespaces = namespaces # of the part that has no value, once known
class _Blank(_Absent):
"""This value, or this call's own result, is there but ``null`` or blank
(or there is none: an empty table, no such row): ``None`` for a ``raw``
function, skipped by ``coalesce``. Raised past the call it came from, it
becomes a plain :class:`_Absent`: the outer call's argument is missing."""
message = "value is null or blank"