Skip to content

How to handle errors thrown from defineCommand / defineEvent handlers? #152

@imissyouso

Description

@imissyouso

Hi!
For example:

export = domain.defineEvent({},  (data, aggregate) => {
    aggregate.set('balance', data.balance);
    aggregate.set('value', data.value);

    throw new FakeError(); // unhandled, something went wrong or TypeError
});

how to handle such type of errors?

Approach listed below doesn't help.

    async handle(data: object) {
        return new Promise((resolve, reject) => {
            try {
                this.domain.handle(data, (err) => {
                    if (err) {
                        reject(err);
                    } else {
                        resolve();
                    }
                });
            } catch (e) {
                reject(e);
            }
        });
    }

This problem is similar for defineViewBuilder of eventdenormalizer. Can't catch errors there too.

another a little question:
Why custom command handler is so bad?

Command Handler (Be careful!!!)
Is your use case not solvable without a custom command handling? Sagas? Micro-Services?

what if I need to make external operation like 3rd party API http call and generate event with resulted data without saving to eventstore. This event then can be handled by sagas to generate command to update required aggregate.

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