Skip to content

Commit

Permalink
Add Got options onto responses and errors (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaltk authored and szmarczak committed Dec 9, 2018
1 parent 8848a7a commit 33b838f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,14 @@ const instance = got.extend({

The response object will typically be a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage), however, if returned from the cache it will be a [response-like object](https://github.com/lukechilds/responselike) which behaves in the same way.

##### request

Type: `Object`

**Note:** This is not a [http.ClientRequest](https://nodejs.org/api/http.html#http_class_http_clientrequest).

- `gotOptions` - The options that were set on this request.

##### body

Type: `string` `Object` *(depending on `options.json`)*
Expand Down Expand Up @@ -662,7 +670,7 @@ The default Got options.

## Errors

Each error contains (if available) `body`, `statusCode`, `statusMessage`, `host`, `hostname`, `method`, `path`, `protocol` and `url` properties to make debugging easier.
Each error contains (if available) `body`, `statusCode`, `statusMessage`, `host`, `hostname`, `method`, `path`, `protocol`, `url`, and `gotOptions` properties to make debugging easier.

In Promise mode, the `response` is attached to the error.

Expand Down
3 changes: 2 additions & 1 deletion source/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class GotError extends Error {
path: options.path,
socketPath: options.socketPath,
protocol: options.protocol,
url: options.href
url: options.href,
gotOptions: options
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions source/request-as-event-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ module.exports = (options, input) => {
response.retryCount = retryCount;
response.timings = timings;
response.redirectUrls = redirects;
response.request = {
gotOptions: options
};

const rawCookies = response.headers['set-cookie'];
if (options.cookieJar && rawCookies) {
Expand Down
15 changes: 15 additions & 0 deletions test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ test('Redirects are cached and re-used internally', async t => {
t.is(firstResponse.body, secondResponse.body);
});

test('Cached response should have got options', async t => {
const endpoint = '/cache';
const cache = new Map();
const options = {
url: s.url + endpoint,
auth: 'foo:bar',
cache
};

await got(options);
const secondResponse = await got(options);

t.is(secondResponse.request.gotOptions.auth, options.auth);
});

test('Cache error throws got.CacheError', async t => {
const endpoint = '/no-store';
const cache = {};
Expand Down
10 changes: 10 additions & 0 deletions test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ test('custom body', async t => {
t.is(error.body, 'not');
});

test('contains Got options', async t => {
const options = {
url: s.url,
auth: 'foo:bar'
};

const error = await t.throwsAsync(got(options));
t.is(error.gotOptions.auth, options.auth);
});

test('no status message is overriden by the default one', async t => {
const error = await t.throwsAsync(got(`${s.url}/no-status-message`));
t.is(error.statusCode, 400);
Expand Down
9 changes: 9 additions & 0 deletions test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,12 @@ test('requestUrl response when sending url as param', async t => {
test('response contains url', async t => {
t.is((await got(s.url)).url, `${s.url}/`);
});

test('response contains got options', async t => {
const options = {
url: s.url,
auth: 'foo:bar'
};

t.is((await got(options)).request.gotOptions.auth, options.auth);
});

0 comments on commit 33b838f

Please sign in to comment.