Take this URL for instance: https://api.eveonline.com/eve/CharacterID.xml.aspx?names=Khan
Using xml2js node.js module you may parse that XML, although it does not look pretty:
var CharacterID = response.eveapi.result[0].rowset[0].row[0].$.characterID;
The app crashed after 2 weeks of running, all because rowset[0] was undefined. Prior to that it crashed because eveapi was not defined. Seriously, does my if-else has to be like this just to prevent server from crashing due to stupid undefined object errors?
if (!response.eveapi ||
!response.eveapi.result[0] ||
!response.eveapi.result[0].rowset[0] ||
!response.eveapi.result[0].rowset[0].row[0]) {
return res.send(500, "Error");
Besides the obvious if (err) return res.send(500, "Error");
error handling where applicable, what is the general practice for undefined errors?
I wrote a library for this kind of thing, called dotty (https://github.com/deoxxa/dotty).
In your case, you could do this:
var dotty = require("dotty");
var CharacterID = dotty.get(response, "eveapi.result.0.rowset.0.row.0.$.characterID");
In the case of the path not being resolvable, it'll just return undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With