voryx / thruway.js

RxJS WAMPv2 Client
MIT License
28 stars 13 forks source link

Best way to throw an exception inside a registered function #24

Closed 27leaves closed 5 years ago

27leaves commented 5 years ago

When I try to throw an Exception like

async function trySomething(a, b) {
  // try something
  throw(new Error('hello'));
}

wamp.register('add.rpc', trySomething).subscribe();

then the Error I get on the other side is

Error {
  error: 'thruway.error.invocation_exception',
  args: [],
  kwargs: {}
}

Is there a way to raise another error or provide details about the Error on side that calls the function?

Thanks!

mbonneau commented 5 years ago

I believe throwing a WampErrorException will allow you to control the WAMP error details that get sent back to the caller.

27leaves commented 5 years ago

Thank you, but for that I would need an InvocationMessage and for that some information that I do not have at that point.

27leaves commented 5 years ago

OK, it works like that

throw new WampInvocationException(
      new InvocationMessage(InvocationMessage.MSG_INVOCATION, 0, {}, args, argskw),
      'my.error.trysomething',
      args,
      argskw,
      {
        error: `Some error description`
      }
    );

that on the other side this releases:

Error {
  error: 'my.error.trysomething',
  args: [],
  kwargs: { code: 'd126' }
}

Seems like the only information that I don't have at that point is the registrationId. Don't I need that? And the details are not passed, but I guess that's by design?

davidwdan commented 5 years ago

It should be easier than that to throw an exception. I’ll take a look at it later today.

On Fri, Apr 19, 2019 at 12:06 PM Johannes Herrnegger < notifications@github.com> wrote:

OK, it works like that

throw new WampInvocationException(new InvocationMessage(InvocationMessage.MSG_INVOCATION, 0, {}, args, argskw), 'my.error.trysomething', args, argskw, { error: Some error description } );

that on the other side this releases:

Error { error: 'my.error.trysomething', args: [], kwargs: { code: 'd126' } }

Seems like the only information that I don't have at that point is the registrationId. Don't I need that? And the details are not passed, but I guess that's by design?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/voryx/thruway.js/issues/24#issuecomment-484943241, or mute the thread https://github.com/notifications/unsubscribe-auth/ABF5FXYMVO3UJZ25NTPX4KDPRHUY3ANCNFSM4HHEYNLQ .

davidwdan commented 5 years ago

This should work:

async function trysomething(a, b) {
    throw new WampErrorException('my.error.trysomething', ['arg1', 'arg2']);
}

How do you plan on using the registrationId?

We limit what is sent back with the error message to limit the possibility of accidentally leaking information. You have fine grain control over everything with WampErrorExeption.

27leaves commented 5 years ago

Oh yes, that should be sufficient.

I do not need the registrationId. I just would have needed it on using WampInvocationException with InvocationMessage.

Thank you! :)