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

statusCode for responses labeled undefined when promise has a statusCode of 200 #1569

Closed
2 tasks done
Tracked by #1684
dominusmars opened this issue Dec 23, 2020 · 3 comments
Closed
2 tasks done
Tracked by #1684
Labels
bug Something does not work as it should ✭ help wanted ✭

Comments

@dominusmars
Copy link

dominusmars commented Dec 23, 2020

Describe the bug

  • Node.js version: v12.19.0.
  • OS & version: windows 10 20H2

Actual behavior

... terminates node.js process due to unhandled error. Stated that statusCode is undefined.

(node:28884) UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property 'statusCode' of 'response' as it is undefined. at Object.exports.isResponseOk (C:\Users\OneandOnly\Desktop\Sensor\node_modules\got\dist\source\core\utils\is-response-ok.js:5:13) at Request.<anonymous> (C:\Users\OneandOnly\Desktop\Sensor\node_modules\got\dist\source\as-promise\index.js:116:39) at processTicksAndRejections (internal/process/task_queues.js:97:5)

Expected behavior

... returns promise statusCode as 200 if the site is live.

Code to reproduce

const site= got.extend({
		method: "GET",
		followRedirect: false,
		headers: {
			"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0",
			accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
			"accept-language": "en-US,en;q=0.9,en",
			"accept-encoding": "gzip, deflate, br",
			Connection: "keep-alive",
			"Upgrade-Insecure-Request": "1",
		},
		hooks: {
			beforeResponse: [(options) => {}],
			afterResponse: [
				(response) => {
					setTimeout(function () {
						fs.writeFileSync("response.txt", response.body);
					}, 1000);
				},
			],
		},
	});
	try {
		var { response } = site.get("https://www.google.com");
		return response;
	} catch (error) {
		console.log(error);
	}

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@szmarczak szmarczak added bug Something does not work as it should ✭ help wanted ✭ labels Dec 24, 2020
@sindresorhus
Copy link
Owner

TypeError: Cannot destructure property 'statusCode' of 'response' as it is undefined.

This indeed a Got bug that crashes here, but your code is also invalid and even if Got didn't crash here, yours would.


afterResponse: [
				(response) => {
					setTimeout(function () {
						fs.writeFileSync("response.txt", response.body);
					}, 1000);
				},
			],

You should not use setTimeout. This will cause an unhandled rejection when the write call fails. Use delay or something similar, make afterResponse an async function, and promisify the fs call.

@sindresorhus
Copy link
Owner

sindresorhus commented Dec 24, 2020

@szmarczak We should add validation that ensures the user returns a response from afterResponse and throw a human-friendly error message if not.


Each function should return the response. - https://github.com/sindresorhus/got#hooksafterresponse

@dominusmars
Copy link
Author

TypeError: Cannot destructure property 'statusCode' of 'response' as it is undefined.

This indeed a Got bug that crashes here, but your code is also invalid and even if Got didn't crash here, yours would.

afterResponse: [
				(response) => {
					setTimeout(function () {
						fs.writeFileSync("response.txt", response.body);
					}, 1000);
				},
			],

You should not use setTimeout. This will cause an unhandled rejection when the write call fails. Use delay or something similar, make afterResponse an async function, and promisify the fs call.

thanks for the advice. was trying stuff out to see what would happen.

@szmarczak szmarczak mentioned this issue Mar 21, 2021
71 tasks
@szmarczak szmarczak mentioned this issue Apr 13, 2021
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something does not work as it should ✭ help wanted ✭
Projects
None yet
Development

No branches or pull requests

3 participants