From 0109725b7406e8b7005a4a435a0617179b1dc2da Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sat, 12 Sep 2015 04:36:55 +0500 Subject: [PATCH] workaround throwing in promise mode Fixes #99 --- index.js | 10 ++++++---- test/test-arguments.js | 9 +++++++-- test/test-error.js | 9 +++++++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 76be965..3ac5d19 100644 --- a/index.js +++ b/index.js @@ -256,14 +256,16 @@ function got(url, opts, cb) { opts = {}; } - opts = normalizeArguments(url, opts); - if (cb) { - asCallback(opts, cb); + asCallback(normalizeArguments(url, opts), cb); return null; } - return asPromise(opts); + try { + return asPromise(normalizeArguments(url, opts)); + } catch (error) { + return PinkiePromise.reject(error); + } } var helpers = [ diff --git a/test/test-arguments.js b/test/test-arguments.js index 8d727cc..121721c 100644 --- a/test/test-arguments.js +++ b/test/test-arguments.js @@ -24,10 +24,15 @@ test('setup', function (t) { }); test('url argument is required', function (t) { + t.plan(2); t.throws(function () { - got(); + got(undefined, function () {}); }, /Parameter `url` must be a string or object, not undefined/); - t.end(); + + got() + .catch(function (err) { + t.ok(/Parameter `url` must be a string or object, not undefined/.test(err.message)); + }); }); test('accepts url.parse object as first argument', function (t) { diff --git a/test/test-error.js b/test/test-error.js index 5e8a672..4997b85 100644 --- a/test/test-error.js +++ b/test/test-error.js @@ -36,10 +36,15 @@ test('dns error message', function (t) { }); test('options.body error message', function (t) { + t.plan(2); t.throws(function () { - got(s.url, {body: function () {}}); + got(s.url, {body: function () {}}, function () {}); }, /options.body must be a ReadableStream, string, Buffer or plain Object/); - t.end(); + + got(s.url, {body: function () {}}) + .catch(function (err) { + t.ok(/options.body must be a ReadableStream, string, Buffer or plain Object/.test(err.message)); + }); }); test('cleanup', function (t) {