Repository navigation
Replies: 3 comments
|
The second parameter of WhyAxios interceptors work as a Promise chain internally. So in your code, How to actually trigger the second parameter// interceptor 1 - intentionally rejects
instance.interceptors.request.use(
(config) => Promise.reject(new Error("rejected from interceptor 1"))
);
// interceptor 2 - second parameter receives the rejection from interceptor 1
instance.interceptors.request.use(
(config) => config,
(error) => {
console.log("now this runs!", error); // runs
return Promise.reject(error);
}
);If you want to catch runtime errors inside a single interceptor, use try/catch explicitly. instance.interceptors.request.use(
(config) => {
try {
let username = null;
console.log(username.length);
} catch (error) {
return Promise.reject(error);
}
return config;
},
(error) => {
console.log("now this runs!", error); // runs
return Promise.reject(error);
}
);My English isn't great, so I used Claude for the translation, there may be some awkward phrasing. Feel free to reply if anything is unclear. |
|
Appreciate the detailed report I will look into this
…On Wed, Mar 18, 2026, 7:12 AM Engy Park ***@***.***> wrote:
The second parameter of request interceptor only runs when a previous
interceptor returns Promise.reject(), not when a runtime error occurs
inside the first parameter.
Why
Axios interceptors work as a Promise chain internally.
The second parameter is a .catch() handler for the *previous
interceptor's rejected promise*, not a try/catch for the current one.
So in your code, username.length throws a TypeError at runtime, which
bypasses the chain entirely and is never caught by the second parameter.
How to actually trigger the second parameter
// interceptor 1 - intentionally rejectsinstance.interceptors.request.use(
(config) => Promise.reject(new Error("rejected from interceptor 1")));
// interceptor 2 - second parameter receives the rejection from interceptor 1instance.interceptors.request.use(
(config) => config,
(error) => {
console.log("now this runs!", error); // runs
return Promise.reject(error);
});
If you want to catch runtime errors inside a single interceptor, use
try/catch explicitly.
instance.interceptors.request.use(
(config) => {
try {
let username = null;
console.log(username.length);
} catch (error) {
return Promise.reject(error);
}
return config;
},
(error) => {
console.log("now this runs!", error); // runs
return Promise.reject(error);
});
My English isn't great, so I used Claude for the translation, there may be
some awkward phrasing. Feel free to reply if anything is unclear.
—
Reply to this email directly, view it on GitHub
<#7375 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/B7Z2X6EHNJKTVIZLW6FW5YT4RJD65AVCNFSM6AAAAACUFVX4XGVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTMMJYHA3DIMY>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
|
The second parameter of the same request interceptor is not a This: instance.interceptors.request.use(
function (config) {
let username = null;
console.log(username.length); // throws TypeError
return config;
},
function (error) {
// This does not catch errors thrown above
console.log('request error', error);
return Promise.reject(error);
}
);works like this internally: Promise.resolve(config).then(onFulfilled, onRejected)In JavaScript promises, the So your .catch(function (error) {
console.log("catch:");
console.log(error);
});To catch that error inside the request interceptor, use instance.interceptors.request.use(
function (config) {
try {
console.log("request before send");
let username = null;
console.log(username.length);
return config;
} catch (error) {
console.log("caught inside request interceptor:", error);
return Promise.reject(error);
}
},
function (error) {
console.log("request interceptor rejected:", error);
return Promise.reject(error);
}
);If you want the second parameter of a request interceptor to run, the request must already be rejected before that interceptor runs. For example, one interceptor rejects, and another interceptor's error handler receives it: instance.interceptors.request.use(function (config) {
return Promise.reject(new Error("stop request"));
});
instance.interceptors.request.use(
function (config) {
return config;
},
function (error) {
console.log("this can run:", error.message);
return Promise.reject(error);
}
);The exact order depends on interceptor registration order, but the key point is the same: the second parameter handles a rejection coming into that interceptor. It does not catch exceptions thrown by the first parameter of the same interceptor. For your example, the final |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
how run into second param??
All reactions