Get remote URL in beforeError hook #1919
-
|
Is there a way to get remote URL in beforeError hook without adding context for each request? For example, on timeout would be handy to log remote URL what caused the timeout. |
Beta Was this translation helpful? Give feedback.
Answered by
sindresorhus
Oct 11, 2025
Replies: 1 comment
-
|
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. |
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
the URL is available on the error object directly. no need to add context manually.
this hook applies to all requests made with the extended client, so you dont need to set it for each request individually.