Using try catch around an object literal

Inside the callback, I create an object that I create to send in my Express application:

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 answer than the one I'm used to, I get a fatal error cannot find 'property' of undefined , which causes my server to crash caused by these: data.actions[1].causes[0].shortDescription .

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

What is considered a good way to handle this?

+6
source share
2 answers

1. use try / catch

In your statement, you:

  • 26 potential sources for reference errors (16 missing objects, 10 arrays beyond the borders).

What handles try / catch best. Work with the same type of errors from a block of code and processing in one place. Therefore, if you do not want to deal with each potential error individually, try / catch the simplest and best tool that you have at your disposal. It works and you can identify the problem:

 var w = {} try {wxyz} catch(e) {e} 

TypeError: Cannot read property 'y' from undefined (...)

Alternatively, if you need more control, you can

2. check all links up

If necessary, even if some properties do not work, you can check each link. eg:.

 var sdValid = data && data.actions && data.actions.length>1 && data.actions[0].causes && data.actions[0].causes.length && data.actions[1].causes[0].shortDescription; var owner = sdValid ? data.actions[1].causes[0].shortDescription : 'default value'; this.response = { owner : owner, // etc... } 

You can easily make it more readable if you

3. expand your own control link

 this.response = { owner : nestget(data, 'nobody') .prop('actions', 1) .prop('causes', 0) .prop('shortDescription') .value(), // return shortDescription if you got this far or 'nobody' build_version : nestget(data, '0.0.0') .prop('actions', 0) .prop('parameters', 0) .prop('value') .value(), // return value if you got this far or '0.0.0' // ... etc }; // Custom - easy to use reference checker function nestget (obj, default) { var valid = false; this.default = default; this.obj = obj; var that = this; return { prop : propChecker, value : getValue } function propChecker (prop, i) { // implementation omitted for brevity } function getValue () { return value; } } 

4. use the library

Last but not least, you can always use the library. Personally, I like the way XPath looks for XML trees, so I would suggest using something like JSONPath to find nested objects in a data structure. eg.

 this.response = { owner : jsonpath(data, '$..shortDescription[0]'), build_version jsonpath(data, '$.actions[0].parameters[0].value'), // no error // ... etc } 

However, there are many options (e.g. lodash / underscore, as indicated in the comment).

+2
source
 try { //unwrap your data } catch(err) { //handle the failure } 

- a good decision, depending on how you want to be a nuance

If you want to continue execution, even if one or more failed, you can consider the conditional assignment operator

javascript: is this a conditional job? .

 this.response = { owner: data.actions[1].causes[0].shortDescription || undefined, etc: data.something || undefined }; 

Of course, you will need to make sure that the material that processes this. Responding upstream will not explode for the same reason.

Edit: The above does not work for me, although I thought it was a javascript feature. If you cannot make it work, you can do the same with the ternary operator

 var asdf = (jkl == undefined) ? 3 : jkl; 

What says

 if(asdf == undefined) asdf = 3; else asdf = jkl 

The problem is that if you do

 var asdf = (nested.variable == undefined) ? 3 : nested.variable; 

Your code will break if nested is undefined . There are several workarounds for this, for example, setting several flags and then coordinating the trinity with them.

I heard (and agree) that nested ternary statements are not a good idea.

Other editing: Check here How to determine if a variable is "undefined" or "null"? for questions regarding variables that may or may not be defined. Apparently == undefined and == null only work if the variables are declared but not defined. In fact, I think so. If in doubt, use typeof .

0
source

All Articles