Skip to content

Exceptions

rn_forge.commons.exceptions

Structured exception base classes for rn-forge-* packages.

Typical usage::

from rn_forge.commons import AppException

AppException.check(user_id, "User id is required", error_code=400)

raise AppException(
    "Unable to process order {}",
    order_id,
    error_code=409,
    customer_id=customer_id,
)

AppException(message, *message_args, error_code=-1, **error_data)

Bases: Exception

Base exception with structured error code and contextual data.

Parameters:

Name Type Description Default
message str

Human-readable description; supports str.format positional args.

required
*message_args Any

Positional arguments forwarded to message.format().

()
error_code int

Application-specific integer error code (default -1).

-1
**error_data Any

Arbitrary key/value context attached to the exception.

{}

Example::

raise AppException(
    "Invalid state transition: {} -> {}",
    old_state,
    new_state,
    error_code=1001,
    order_id=order_id,
)

check(value, message, *message_args, **kwargs) classmethod

Raise this exception if value is falsy.

Parameters:

Name Type Description Default
value Any

The value to test.

required
message str

Exception message (supports positional format args).

required
*message_args Any

Forwarded to message.format().

()
**kwargs Any

Forwarded as error_code / error_data to the constructor.

{}

Example::

AppException.check(path.exists(), "Missing file: {}", path, error_code=404)