Skip to content
Discussion options

You must be logged in to vote

Try-catch blocks should work fine with Got. Make sure you're using await and wrapping it properly:

import got from 'got';

try {
	const response = await got('https://example.com/invalid-endpoint');
	console.log(response.body);
} catch (error) {
	console.error('Request failed:', error.message);
	console.error('Status code:', error.response?.statusCode);
}

If you're using promises without async/await, use .catch():

got('https://example.com/invalid-endpoint')
	.then(response => {
		console.log(response.body);
	})
	.catch(error => {
		console.error('Request failed:', error.message);
	});

If your app is still crashing, it might be because:

  1. You're not awaiting the promise
  2. You have an unhandled…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants