Skip to content
Discussion options

You must be logged in to vote

the URL is available on the error object directly. no need to add context manually.

import got from 'got';

const client = got.extend({
	hooks: {
		beforeError: [
			error => {
				// the URL is always available here
				const url = error.options.url?.toString();
				console.log('Request failed for:', url);
				
				// if theres a response, you can also get:
				// - original URL: error.response?.requestUrl
				// - final URL (after redirects): error.response?.url
				
				return error;
			}
		]
	}
});

this hook applies to all requests made with the extended client, so you dont need to set it for each request individually.

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