Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify docs for got.HTTPError #1244

Merged
merged 3 commits into from May 10, 2020
Merged

Conversation

chinesedfan
Copy link
Contributor

@chinesedfan chinesedfan commented May 10, 2020

Checklist

  • I have read the documentation.
  • I have included a pull request description of my changes.
  • I have included some tests.
  • If it's a new feature, I have included documentation updates.

Closed #1213. The preview is as following, and note that the anchor is a relative link so it doesn't work here but doesn't matter in fact.


Determines if a got.HTTPError is thrown for error responses.

got.ParseError

ok: 2xx or 304, and includes other 3xx if options.followRedirect is set

When server response code is ok, and parsing body fails. Includes a response property.

got.HTTPError

When the server response code is not ok. Includes a response property.

readme.md Outdated Show resolved Hide resolved
readme.md Outdated
When server response code is 2xx, and parsing body fails. Includes a `response` property.
> *ok*: 2xx or 304, and includes other 3xx if `options.followRedirect` is set

When server response code is ***ok***, and parsing body fails. Includes a `response` property.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually does not affect ParseError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@szmarczak Not really. See source/as-promise/index.ts.

// Parse body
try {
    response.body = parseBody(response, options.responseType, options.encoding);
} catch (error) {
    // Fallback to `utf8`
    response.body = rawBody.toString();

    if (isOk()) {
        // TODO: Call `request._beforeError`, see https://github.com/nodejs/node/issues/32995
        reject(error);
        return;
    }
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have any effect. This code is not executed on redirects. 304 are empty responses, so even with responseType: 'json' it never throws:

if (responseType === 'json') {
return rawBody.length === 0 ? '' : JSON.parse(rawBody.toString()) as unknown;
}

There is no response event emitted on redirect:

got/source/core/index.ts

Lines 1032 to 1114 in ece94ed

if (options.followRedirect && response.headers.location && redirectCodes.has(statusCode)) {
// We're being redirected, we don't care about the response.
// It'd be besto to abort the request, but we can't because
// we would have to sacrifice the TCP connection. We don't want that.
response.resume();
if (this[kRequest]) {
this[kCancelTimeouts]!();
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this[kRequest];
this[kUnproxyEvents]();
}
const shouldBeGet = statusCode === 303 && options.method !== 'GET' && options.method !== 'HEAD';
if (shouldBeGet || !options.methodRewriting) {
// Server responded with "see other", indicating that the resource exists at another location,
// and the client should request it from that location via GET or HEAD.
options.method = 'GET';
if ('body' in options) {
delete options.body;
}
if ('json' in options) {
delete options.json;
}
if ('form' in options) {
delete options.form;
}
}
if (this.redirects.length >= options.maxRedirects) {
this._beforeError(new MaxRedirectsError(this));
return;
}
try {
// Handles invalid URLs. See https://github.com/sindresorhus/got/issues/604
const redirectBuffer = Buffer.from(response.headers.location, 'binary').toString();
const redirectUrl = new URL(redirectBuffer, url);
const redirectString = redirectUrl.toString();
decodeURI(redirectString);
// Redirecting to a different site, clear sensitive data.
if (redirectUrl.hostname !== url.hostname) {
if ('host' in options.headers) {
delete options.headers.host;
}
if ('cookie' in options.headers) {
delete options.headers.cookie;
}
if ('authorization' in options.headers) {
delete options.headers.authorization;
}
if (options.username || options.password) {
delete options.username;
delete options.password;
}
}
this.redirects.push(redirectString);
options.url = redirectUrl;
for (const hook of options.hooks.beforeRedirect) {
// eslint-disable-next-line no-await-in-loop
await hook(options, typedResponse);
}
this.emit('redirect', typedResponse, options);
await this._makeRequest();
} catch (error) {
this._beforeError(error);
return;
}
return;
}

readme.md Outdated Show resolved Hide resolved
readme.md Outdated

#### got.HTTPError

When the server response code is not 2xx. Includes a `response` property.
When the server response code is not ***ok***. Includes a `response` property.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to replace not ok with a more descriptive information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to make ok as a term, as well as used in ParseError.

Copy link
Collaborator

@szmarczak szmarczak May 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep things simple. Don't redefine things. Simply:

When the server response code is not 2xx nor 3xx if options.followRedirect is true.

Copy link
Collaborator

@szmarczak szmarczak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^^

chinesedfan and others added 2 commits May 10, 2020 22:24
Co-authored-by: Szymon Marczak <36894700+szmarczak@users.noreply.github.com>
@szmarczak
Copy link
Collaborator

Looks good. Thanks for submitting! 🙌

@szmarczak szmarczak merged commit 3f125f1 into sindresorhus:master May 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Note that 304 doesn't throw HTTPError
2 participants