Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I was struggling with a way to create conditional error messages in javascript. I just finished coming up with the solution below and I'd love to get some feedback on the idea. There no actual library or functions, just a spec. Also on GIST. All of this stemmed from my really bad and unfocused stackoverflow question (Comprehensive conditional error messages).

Is this cool? Is anyone else doing this?

The variable & output:

var _ = require("underscore");
var dotty = require("dotty");

//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*/

The code itself:

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);

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);
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.