Pragmatism in the real world

Error handling in OpenWhisk actions

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.

  • 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 give you make 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!