Skip to content

Instantly share code, notes, and snippets.

@reggi
Last active December 15, 2015 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reggi/5202723 to your computer and use it in GitHub Desktop.
Save reggi/5202723 to your computer and use it in GitHub Desktop.
This is one way to manage errors.
/* More complicated use-case */
var v = {};
v.toru = (typeof req !== "undefined") ? 0 : "variable req is undefined";
v.derb = (!v.toru && dotty.exists(req, "body")) ? 0 : "req.body does not exist";
v.derh = (!v.toru && dotty.exists(req, "headers")) ? 0 : "req.headers does not exist";
v.dehu = (!v.derh && dotty.exists(req, "headers.user-agent")) ? 0 : "req.headers.user-agent does not exist";
v.deht = (!v.derh && dotty.exists(req, "headers.x-shopify-topic")) ? 0 : "req.headers.x-shopify-topic does not exist";
v.dehd = (!v.derh && dotty.exists(req, "headers.x-shopify-shop-domain")) ? 0 : "req.headers.x-shopify-shop-domain does not exist";
v.dehs = (!v.derh && dotty.exists(req, "headers.x-shopify-hmac-sha256")) ? 0 : "req.headers.x-shopify-hmac-sha256 does not exist";
v.rhur = (!v.dehu && (req.headers["user-agent"] == "Ruby")) ? 0 : "User-Agent isn't set to Ruby";
var errors = function(strip){ return (strip.length == 0) ? false : strip; }(_.without(_.values(v), 0));
console.log(errors);
var _ = require("underscore");
//var hello = undefined;
/*[ 'variable hello is undefined',
'variable hello is not a string',
'variable hello is not true',
'variable hello is not equal to world' ]*/
//--------------------
//var hello = true;
/*[ 'variable hello is not a string',
'variable hello is not equal to world' ]*/
//--------------------
//var hello = false;
/*[ 'variable hello is not a string',
'variable hello is not true',
'variable hello is not equal to world' ]*/
//--------------------
//var hello = 3421;
/*[ 'variable hello is not a string',
'variable hello is not equal to world' ]*/
//--------------------
//var hello = "jupiter";
/*[ 'variable hello is not equal to world' ]*/
//--------------------
//var hello = "world";
/*false*/
var v = {};
v.tohu = (typeof hello !== "undefined") ? 0 : "variable hello is undefined";
v.tohs = (typeof hello == "string") ? 0 : "variable hello is not a string";
v.htru = (!v.tohu && hello) ? 0 : "variable hello is not true";
v.hewo = (!v.htru && hello == "world") ? 0 : "variable hello is not equal to world";
var errors = function(strip){ return (strip.length == 0) ? false : strip; }(_.without(_.values(v), 0));
console.log(errors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment