Skip to content

errors

errors

JSON-RPC 2.0 error codes and helper functions for Marianne daemon IPC.

Maps the daemon exception hierarchy (DaemonError and subclasses) to standard and Marianne-extension JSON-RPC error codes, plus convenience builders for the most common error responses.

Classes

Functions

make_error

make_error(code, message, request_id, data=None)

Build a JsonRpcError with the given code and message.

Source code in src/marianne/daemon/ipc/errors.py
def make_error(
    code: int,
    message: str,
    request_id: int | str | None,
    data: dict[str, Any] | None = None,
) -> JsonRpcError:
    """Build a ``JsonRpcError`` with the given code and message."""
    return JsonRpcError(
        error=ErrorDetail(code=code, message=message, data=data),
        id=request_id,
    )

parse_error

parse_error(request_id=None)

Malformed JSON received.

Source code in src/marianne/daemon/ipc/errors.py
def parse_error(request_id: int | str | None = None) -> JsonRpcError:
    """Malformed JSON received."""
    return make_error(PARSE_ERROR, "Parse error: malformed JSON", request_id)

invalid_request

invalid_request(request_id, detail='')

Missing jsonrpc, method, or wrong types.

Source code in src/marianne/daemon/ipc/errors.py
def invalid_request(
    request_id: int | str | None, detail: str = ""
) -> JsonRpcError:
    """Missing ``jsonrpc``, ``method``, or wrong types."""
    msg = "Invalid request"
    if detail:
        msg = f"{msg}: {detail}"
    return make_error(INVALID_REQUEST, msg, request_id)

method_not_found

method_not_found(request_id, method)

Unknown RPC method name.

Source code in src/marianne/daemon/ipc/errors.py
def method_not_found(
    request_id: int | str | None, method: str
) -> JsonRpcError:
    """Unknown RPC method name."""
    return make_error(
        METHOD_NOT_FOUND,
        f"Method not found: {method}",
        request_id,
        data={"method": method},
    )

invalid_params

invalid_params(request_id, detail)

Params failed Pydantic validation.

Source code in src/marianne/daemon/ipc/errors.py
def invalid_params(
    request_id: int | str | None, detail: str
) -> JsonRpcError:
    """Params failed Pydantic validation."""
    return make_error(INVALID_PARAMS, f"Invalid params: {detail}", request_id)

internal_error

internal_error(request_id, detail='')

Unexpected server exception.

Source code in src/marianne/daemon/ipc/errors.py
def internal_error(
    request_id: int | str | None, detail: str = ""
) -> JsonRpcError:
    """Unexpected server exception."""
    msg = "Internal error"
    if detail:
        msg = f"{msg}: {detail}"
    return make_error(INTERNAL_ERROR, msg, request_id)

map_exception_to_rpc_error

map_exception_to_rpc_error(exc, request_id)

Convert a DaemonError into the appropriate JsonRpcError.

Source code in src/marianne/daemon/ipc/errors.py
def map_exception_to_rpc_error(
    exc: DaemonError, request_id: int | str | None
) -> JsonRpcError:
    """Convert a ``DaemonError`` into the appropriate ``JsonRpcError``."""
    code = _EXCEPTION_CODE_MAP.get(type(exc), INTERNAL_ERROR)
    return make_error(code, str(exc), request_id)

rpc_error_to_exception

rpc_error_to_exception(error)

Convert a JSON-RPC error dict into the appropriate DaemonError.

Source code in src/marianne/daemon/ipc/errors.py
def rpc_error_to_exception(error: dict[str, Any]) -> DaemonError:
    """Convert a JSON-RPC error dict into the appropriate ``DaemonError``."""
    code = error.get("code", INTERNAL_ERROR)
    message = error.get("message", "Unknown error")
    exc_cls = _CODE_EXCEPTION_MAP.get(code, DaemonError)
    return exc_cls(message)