Add error checking

When the input block number count is larger than the current block number, we can raise the exception to stop our model run. The exception type we can raise is ModelRunError imported from credmark.cmf.model.errors.

from credmark.cmf.model import Model
from credmark.dto import DTO
from credmark.cmf.model.errors import ModelRunError


class ApeCountInput(DTO):
    block_number_count: int = DTOField(1, gt=0,
                                    description='Number of blocks to look back')


@Model.describe(slug='contrib.ape-count',
                version='1.0',
                display_name='Count how many transactions in the one block',
                description="Use ledger and some counts",
                input=ApeCountInput,
                output=dict)
class ContribModel(Model):
    def run(self, input: ApeCountInput) -> dict:
        if input.block_number_count > self.context.block_number:
            raise ModelRunError(f'input {input.block_number_count=} is larger than '
                                f'the current block numer ${self.context.block_number}')
        return {}

More details on the Error Handling can be found here.

Last updated