Skip to content

Hook beforeError does not respect the throwHttpErrors setting #2103

@Supinic

Description

@Supinic

Describe the bug

  • Node.js version: 18.0.0
  • OS & version: Windows 10, Raspbian Buster, Ubuntu 20.04
  • Got version: 11.6.0 and higher

Actual behavior

The beforeError hook fires regardless of whether or not the throwHttpErrors option is set to false and the request fails. This has been (erroneously, it seems?) changed in #1384 - more specifically in as-promise/index.ts. This change wasn't mentioned anywhere in the 11.6.0 release, or in other releases since.

The behaviour is correct in all versions up until 11.6.0, where this PR was merged.

Expected behavior

The beforeError hook should not fire if the throwHttpErrors option is set to false, and the request fails.

Code to reproduce

const got = require("got");
const opts = {
    throwHttpErrors: false,
    hooks: {
        beforeError: [
            (e) => {
                console.log("beforeError hook", e.name);
            }
        ]
    }
};

(async () => {
    const res = await got.get("https://httpstat.us/400", opts);
    console.log("Response", res.body);
})();

In versions prior to 11.6.0, the request response will be logged into console. In versions since, the beforeError hook will take precedence, log the error from it, and the response will not be logged.

As an alternative to a code-based fix, the documentation could be adjusted instead, as it currently contains no reference to such behaviour.

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething does not work as it shoulddocumentationThe issue will improve the docs

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions