Skip to content

Commit

Permalink
Throw an error when generating an unrecognizable route.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanhammond committed Oct 5, 2016
1 parent 51e823b commit b10bc46
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/route-recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ DynamicSegment.prototype = {
},

generate: function(params) {
var value = params[this.name];
if (value.length === 0) {
throw new Error("You must provide a param `" + this.name + "`.");
}

if (RouteRecognizer.ENCODE_AND_DECODE_PATH_SEGMENTS) {
return encodePathSegment(params[this.name]);
return encodePathSegment(value);
} else {
return params[this.name];
return value;
}
}
};
Expand All @@ -82,7 +87,12 @@ StarSegment.prototype = {
},

generate: function(params) {
return params[this.name];
var value = params[this.name];
if (value.length === 0) {
throw new Error("You must provide a param `" + this.name + "`.");
}

return value;
}
};

Expand Down
13 changes: 13 additions & 0 deletions tests/recognizer-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,19 @@ globGenerationValues.forEach(function(value) {
});
});

test("throw when generating dynamic routes with empty string", function() {
var router = new RouteRecognizer();
router.add([{"path":"/posts","handler":"posts"},{"path":"/*secret/create","handler":"create"}], { as: "create"})
router.add([{"path":"/posts","handler":"posts"},{"path":"/:secret/edit","handler":"edit"}], { as: "edit"})

QUnit.throws(function() {
router.generate("create", { secret: "" });
}, /You must provide a param `secret`./);
QUnit.throws(function() {
router.generate("edit", { secret: "" });
}, /You must provide a param `secret`./);
});

test("Parsing and generation results into the same input string", function() {
var query = "filter%20data=date";
equal(router.generateQueryString(router.parseQueryString(query)), '?' + query);
Expand Down

0 comments on commit b10bc46

Please sign in to comment.