How to properly catch error when endpoint fails #2245
-
Beta Was this translation helpful? Give feedback.
Answered by
sindresorhus
Oct 11, 2025
Replies: 1 comment
-
|
Try-catch blocks should work fine with Got. Make sure you're using 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 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:
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sindresorhus
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Try-catch blocks should work fine with Got. Make sure you're using
awaitand wrapping it properly:If you're using promises without async/await, use
.catch():If your app is still crashing, it might be because: