thenativeweb / node-cqrs-domain

Node-cqrs-domain is a node.js module based on nodeEventStore that. It can be very useful as domain component if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.
http://cqrs.js.org/pages/domain.html
MIT License
269 stars 57 forks source link

Where to catch Error: Please pass a valid aggregate id! #147

Closed bikerp closed 5 years ago

bikerp commented 5 years ago

Hi, I use Promise to handle command:

function handleCommand(cmd) {
    return new Promise((resolve, reject) => {
        domain.handle(cmd, err => {
            if (err) {
                reject(err);
            }
            resolve();
        });
    });
}

However the problem is when Error: Please pass a valid aggregate id! occurs then the whole process exits and the callback function is not called. Is this a bug or I'm doing it wrong way?

Thanks

adrai commented 5 years ago

try like this:

function handleCommand(cmd) {
    return new Promise((resolve, reject) => {
        try {
          domain.handle(cmd, err => {
              if (err) {
                  reject(err);
              }
              resolve();
          });
        } catch (e) { reject(e); }
    });
}
nanov commented 5 years ago

Just a small addition so you won't resolve an already rejected promise:

function handleCommand(cmd) {
  return new Promise((resolve, reject) => {
    try {
      domain.handle(cmd, (err) => {
        if (err)
          return reject(err);
        resolve();
      });
     } catch (e) { reject(e) }
  });
}

note the return statement on the first reject.

adrai commented 5 years ago

right, didn’t see it

bikerp commented 5 years ago

Hi @adrai, thanks for the feedback. I tried the try/catch but even this didn't help.

nanov commented 5 years ago

@bikerp This is rather strange, could you post the stack trace of the error?

bikerp commented 5 years ago
Error: Please pass a valid aggregate id!

    at DefaultCommandHandler.getNextCommandInQueue (C:\Users\bikerp\projects\test\src\commands\node_modules\cqrs-domain\lib\defaultCommandHandler.js:112:17)
    at _handle (C:\Users\bikerp\projects\test\src\commands\node_modules\cqrs-domain\lib\defaultCommandHandler.js:1133:27)
    at DefaultCommandHandler.handle (C:\Users\bikerp\projects\test\src\commands\node_modules\cqrs-domain\lib\defaultCommandHandler.js:1163:14)
    at CommandDispatcher.dispatch (C:\Users\bikerp\projects\test\src\commands\node_modules\cqrs-domain\lib\commandDispatcher.js:119:29)
    at C:\Users\bikerp\projects\test\src\commands\node_modules\cqrs-domain\lib\domain.js:670:30
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
nanov commented 5 years ago

Ok, i saw where the problem is. Will submit a fix!

nanov commented 5 years ago

Fixed in v2.14.73.

adrai commented 5 years ago

good catch

nanov commented 5 years ago

@bikerp Could you confirm that solves your issue? So we could close it on our side?

bikerp commented 5 years ago

@adrai Good job. It works. Even without the try/catch. Thanks for the quick fix