Skip to content

Raise detailed/accurate exceptions in Python #30

@smartgoo

Description

@smartgoo

Currently, all exceptions raised in Python are either type Exception or KeyError.

There are many areas where raising Python OOB (e.g. ValueError) exceptions or custom exceptions would be highly beneficial for SDK developers. Particularly around transaction creation and submission, RPC, etc. Accurate exceptions would provide the ability to catch and handle specific issues.

PyO3 provides two mechanisms that can help here:

  • Structs for OOB Python exceptions in pyo3::exceptions.
  • The ability to define and raise custom exceptions from the bound Rust to Python. This is detailed in the Python exceptions page of the PyO3 user guide.

An (incomplete) task list:

  • Create submodule exceptions. This provides a location to store custom exceptions (import X from kaspa.exceptions). Completed in PR Add exceptions submodule  #23.
  • Create macro for defining custom exceptions. While PyO3 provides a macro for creating exceptions, it does not support adding doc strings to the new macro. Hence, our own macro is useful. Completed in PR Add macro for defining custom Python exceptions #25.
  • Ensure all dictionary parsing functions raise a KeyError if a required key is not present.
  • Identify custom exceptions that should be raised and convert code to use newly created custom exceptions. These should probably very closely (if not exactly) mirror the underlying errors defined in rusty-kaspa.

This is breaking and should probably be handled in one body of work to the extent possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions