fix: refine stacktrace attribution of errors thrown from middleware (#21470)

* Refine middleware types to include the method signature and to express the nullability of the middleware.

* Make sure that the stacktrace does not involve middleware unless the error originated from the middleware itself.

Co-authored-by: steveluscher <github@steveluscher.com>
This commit is contained in:
Steven Luscher
2021-11-28 21:43:33 -08:00
committed by GitHub
parent 88ef24cecb
commit d36ff8d978
2 changed files with 42 additions and 8 deletions

View File

@@ -760,19 +760,24 @@ function createRpcClient(
agentManager = new AgentManager(useHttps);
}
let fetchWithMiddleware: (url: string, options: any) => Promise<Response>;
let fetchWithMiddleware:
| ((url: string, options: any) => Promise<Response>)
| undefined;
if (fetchMiddleware) {
fetchWithMiddleware = (url: string, options: any) => {
return new Promise<Response>((resolve, reject) => {
fetchMiddleware(url, options, async (url: string, options: any) => {
fetchWithMiddleware = async (url: string, options: any) => {
const modifiedFetchArgs = await new Promise<[string, any]>(
(resolve, reject) => {
try {
resolve(await fetch(url, options));
fetchMiddleware(url, options, (modifiedUrl, modifiedOptions) =>
resolve([modifiedUrl, modifiedOptions]),
);
} catch (error) {
reject(error);
}
});
});
},
);
return await fetch(...modifiedFetchArgs);
};
}
@@ -1972,7 +1977,7 @@ export type HttpHeaders = {[header: string]: string};
export type FetchMiddleware = (
url: string,
options: any,
fetch: Function,
fetch: (modifiedUrl: string, modifiedOptions: any) => void,
) => void;
/**