Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Error Handling in OpenWhisk Actions

DZone's Guide to

Error Handling in OpenWhisk Actions

In this post, MVB Rob Allen gives a quick tutorial on how to handle errors in OpenWhisk by using a few simple lines of code.

· Web Dev Zone ·
Free Resource

Learn how Crafter’s Git-based content management system is reinventing modern digital experiences. Download this white paper now. 

With a standard OpenWhisk action, we return a dictionary of the data and let OpenWhisk deal with converting it to JSON, etc. OpenWhisk will also set the correct 200 status code.

How do we handle an error, though?

It turns out that if there is a key called "error" in our returned dictionary, then all the other data is ignored and an error is sent back.

To show this, consider this action:

func main(args: [String:Any]) -> [String:Any] {

    return [
        "name": "Rob Allen",
        "error": "An error occurred"
    ]

}


If we call it from the command line:

$ wsk action invoke --blocking --result test
error: Unable to invoke action 'test': An error occurred (code 0)


For more details, we can look at the full response by leaving out the --result parameter:

$ wsk action invoke --blocking test
ok: invoked /_/test with id 63845d2af1314a23b8bfbf433f6a0d75
{
... stuff ...
    "response": {
        "result": {
            "error": "An error occurred"
        },
        "status": "application error",
        "success": false
    },
    ... more stuff ...
}


A lot of data is returned, but we're only interested in the response section.

  • The result contains the returned data. Note that all other keys are stripped, so only "error" remains.
  • Success is a boolean and is only set to true if the action executed and returned a dictionary that didn't have an "error" key.
  • Status is a string that can be one of:
    • "success": everything is okay (status is true).
    • "application error": Action ran, but there was an error that was handled by OpenWhisk (status is false).
    • "action developer error": A container error occurred (e.g. failed to start action) (status is false).
    • "whisk internal error": An internal system error occurred (status is false).

If we access the action via the API Gateway, then we also get the same output:

$ wsk api-experimental create /test GET test
$ curl -i https://00661c6d-5162-42de-9678-760e352b009c-gws.api-gw.mybluemix.net/test
HTTP/1.1 502 Bad Gateway
Server: nginx
Date: Mon, 13 Mar 2017 08:18:13 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 33

{
  "error": "An error occurred"
}


This gives us the same error message, but the HTTP status is 502 and we can't change it (yet?!).

Web Actions behave differently, though:

$ curl -i "https://openwhisk.ng.bluemix.net/api/v1/experimental/web/19FT_dev/default/test.json"
HTTP/1.1 400 Bad Request
Server: nginx/1.11.10
Date: Mon, 13 Mar 2017 08:20:08 GMT
Content-Type: application/json; charset=UTF-8

{
  "error": "Response is not valid 'application/json'.",
  "code": 2181191
}


However, we can control the HTTP status code and message with a web action as I've discussed, by sending back a dictionary with code, headers, and body keys.

As a result, I think that the additional control that Web Actions gives you makes it compelling to use as the external interface to your actions (i.e. anything that a web hook calls).

Whatever you do, don't use a key called error with a Web Action!

Crafter CMS is a modern Git-based platform for building innovative websites and content-rich digital experiences. Download this white paper now.

Topics:
web dev ,openwhisk ,error handling

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}