DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Error Handling in OpenWhisk Actions

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.

Rob Allen user avatar by
Rob Allen
·
Mar. 17, 17 · Web Dev Zone · Tutorial
Like (1)
Save
Tweet
5.63K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Data (computing) Dictionary (software) Hook Strings JSON Interface (computing) Command (computing)

Published at DZone with permission of Rob Allen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS
  • API Security Weekly: Issue 165
  • How BDD Works Well With EDA
  • How to Gain Competitive Advantage in Software Development With Innovative Technology?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo