diff --git a/lib/url.js b/lib/url.js index 00e4ec9d7be207..3091aaed572852 100644 --- a/lib/url.js +++ b/lib/url.js @@ -353,7 +353,13 @@ function urlFormat(obj) { // this way, you can call url_format() on strings // to clean up potentially wonky urls. if (typeof obj === 'string') obj = urlParse(obj); - if (!(obj instanceof Url)) return Url.prototype.format.call(obj); + + else if (typeof obj !== 'object' || obj === null) + throw new TypeError("Parameter 'urlObj' must be an object, not " + + obj === null ? 'null' : typeof obj); + + else if (!(obj instanceof Url)) return Url.prototype.format.call(obj); + return obj.format(); } diff --git a/test/parallel/test-url.js b/test/parallel/test-url.js index 7b46fc96d5acd1..77d2242ff4b071 100644 --- a/test/parallel/test-url.js +++ b/test/parallel/test-url.js @@ -1557,3 +1557,20 @@ relativeTests2.forEach(function(relativeTest) { 'format(' + relativeTest[1] + ') == ' + expected + '\nactual:' + actual); }); + + + +// https://github.com/iojs/io.js/pull/1036 +var throws = [ + undefined, + null, + true, + false, + 0, + function () {} +]; +for (var i = 0; i < throws.length; i++) { + assert.throws(function () { url.format(throws[i]); }, TypeError); +}; +assert(url.format('') === ''); +assert(url.format({}) === '');