How best to "return an error" from a Worklight procedure?

281 Views Asked by At

I have written an adapter with a few simple procedures. In some circumstances I need to signal the caller that something went wrong. I have tried a few approaches ..

  • Throwing an exception: the text of the exception gets back to the caller via the onFailure callback (great), but comes wrapped in the module name and line number of the exception. TMI.

  • Returning an object where isSuccessful = false: this works like a charm and it's delivered to the caller via the onFailure callback.

For example:

return {
    isSuccessful: false,
    errors: ["No servers available"]
};

This article from IBM however explicitly warns against doing just this, though doesn't describe an alternative - can you?

1

There are 1 best solutions below

0
On

Have you looked at this blog post?
https://www.ibm.com/developerworks/community/blogs/worklight/entry/handling_backend_responses_in_adapters?lang=en

The blog post details:

For invokeProcedure (client to adapter):

  • What does an invocation response look like?
  • When will isSuccessful be true?
  • When will isSuccessful be false?

For invokeHttp (adapter to server):

  • What does a backend invocation response look like?
  • When will isSuccessful be true?
  • When will isSuccessful be false?

Based on the conditions you will provide in the response to the client, you can then return errors in more clarity.