|
| 1 | +"""The exceptions komet-node raises internally. |
| 2 | +
|
| 3 | +Collected in one module so no layer has to reach into another purely for an error type |
| 4 | +(the encoder, for instance, must not depend on the interpreter just to signal a bad input). |
| 5 | +
|
| 6 | +- :class:`NodeInterpreterError` — the K interpreter subprocess failed. |
| 7 | +- :class:`TransactionEncodingError` — a Stellar transaction could not be encoded. |
| 8 | +- :class:`RpcError` — a JSON-RPC error to hand back to the client. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | + |
| 14 | +class NodeError(RuntimeError): |
| 15 | + """Base class for komet-node's own errors.""" |
| 16 | + |
| 17 | + |
| 18 | +class NodeInterpreterError(NodeError): |
| 19 | + """The K interpreter subprocess failed or produced no usable output.""" |
| 20 | + |
| 21 | + |
| 22 | +class TransactionEncodingError(NodeError): |
| 23 | + """A Stellar transaction could not be encoded into a node request envelope. |
| 24 | +
|
| 25 | + Raised by :class:`~komet_node.transaction.TransactionEncoder` for inputs it cannot |
| 26 | + translate (sub-stroop XLM amounts, malformed strkey addresses). On the admission path |
| 27 | + the server catches it and turns it into a ``txMALFORMED`` status response. |
| 28 | + """ |
| 29 | + |
| 30 | + |
| 31 | +class RpcError(Exception): |
| 32 | + """A JSON-RPC error to return to the client, carrying its spec code and message. |
| 33 | +
|
| 34 | + Raised by request validation and dispatch in the server so the envelope builders can |
| 35 | + return a single value type instead of threading a pre-formatted error string back |
| 36 | + through their return type. :meth:`~komet_node.server.StellarRpcServer.handle_rpc` |
| 37 | + catches it and formats the error envelope. The classmethods name the JSON-RPC 2.0 |
| 38 | + error codes; ``invalid_params`` prepends the conventional ``Invalid params:`` prefix. |
| 39 | + """ |
| 40 | + |
| 41 | + code: int |
| 42 | + message: str |
| 43 | + |
| 44 | + def __init__(self, code: int, message: str) -> None: |
| 45 | + super().__init__(code, message) |
| 46 | + self.code = code |
| 47 | + self.message = message |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def invalid_request(cls, message: str) -> RpcError: |
| 51 | + return cls(-32600, message) |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def invalid_params(cls, detail: str) -> RpcError: |
| 55 | + return cls(-32602, f'Invalid params: {detail}') |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def method_not_found(cls, message: str = 'Method not found') -> RpcError: |
| 59 | + return cls(-32601, message) |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def internal(cls, message: str = 'Internal error') -> RpcError: |
| 63 | + return cls(-32603, message) |
0 commit comments