Categories
express javascript node.js

Using a try catch around an object literal

Inside a callback I build an object I build to send out in my Express app:

this.response = {
owner: data.actions[1].causes[0].shortDescription,
build_version: data.actions[0].parameters[0].value,
branch_to_merge: data.actions[0].parameters[1].value,
jira_tickets: data.actions[0].parameters[2].value,
build_description: data.actions[0].parameters[3].value,
outcome: data.result
};

If I get a different response than what I’m used to I get a fatal cannot find 'property' of undefined error that crashes my server caused by these: data.actions[1].causes[0].shortDescription.

I was wondering what to do about it and I thought I could just put it in a try catch but that seems wrong.

What’s considered a good way of dealing with this?