sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.27k stars 935 forks source link

JSON shortcut does not work when using throwHttpErrors: false #1540

Closed ikorolev93 closed 3 years ago

ikorolev93 commented 3 years ago

Describe the bug

When the server returns an error code with JSON body, and I try to parse it using .json() and {throwHttpErrors: false}, I get the following error:

TypeError: Cannot read property 'request' of undefined
    at /path/to/my/project/node_modules/got/dist/source/as-promise/index.js:157:48
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

Actual behavior

TypeError is thrown

Expected behavior

Returning the body of response, parsed as JSON (matching the behavior of {responseType: 'json', resolveOnlyBody: true}, as documented)

Code to reproduce

https://runkit.com/embed/3tt06wxjbio1

const got = require("got");
await got("https://api.github.com/user", {throwHttpErrors: false}).json();

Checklist

akhll commented 3 years ago

why have you closed this issue ? actually i have the same problem now with got version 11.8.2

const host = 'your_host';
const opts: OptionsOfJSONResponseBody = {
     ...,
     throwHttpErrors: false,
     ...,
}
const res = await got(host, opts).json<ExpectedResponseType>()

Code above throws exception

TypeError: Cannot read property 'request' of undefined
    at /path/to/my/project/node_modules/got/dist/source/as-promise/index.js:157:48
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
szmarczak commented 3 years ago

why have you closed this issue ?

Screenshot_20210610-170920_Chrome

actually i have the same problem now with got version 11.8.2

Please open another issue with the full code to reproduce the bug.