Exceptions

Exception classes for P2P, Wallet and NGNT

On the request level, the HTTPError exception when caught in the except block is raised by one of the built-in custom exception classes.

These built-in custom classes help the developer to understand the source of the error. The built-in custom classes are:

  • ServerError

  • AccountError

  • ClientError

  • WalletError

  • QueryError

  • P2PError

The custom classes are built with this template:

class ClassError(Exception):
    def __init__(self, *args):
        if args:
            self.message = args[0]
            self.status_code = args[1]
        else:
            self.message = None
            self.status_code = 404

    @property
    def response(self):
        return {
            "status": "error",
            "name": "ClassError",
            "code": self.status_code,
            "message": self.message
        }

where ClassError is one of the available exception classes in the library.

Example

To return the appropriate error response for an invalid query, the library executes this block of code:

if not query or query == "":
    raise QueryError("Invalid query passed!", 400)

try:
    # some block of code
except QueryError as e: # Catch the earlier raised exception.
    return e.response # { status: "error", "name": "QueryError", "code": 400, message: "Invalid query passed" }

Last updated