Skip to content

Commit

Permalink
workaround throwing in promise mode
Browse files Browse the repository at this point in the history
Fixes #99
  • Loading branch information
floatdrop committed Sep 11, 2015
1 parent 0bf55bb commit 738a2a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 Promise.reject(error);
}
}

var helpers = [
Expand Down
9 changes: 7 additions & 2 deletions test/test-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions test/test-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 738a2a8

Please sign in to comment.