Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ export class FetchFactory {
res = await UndiciFetch(input, init);
});
} catch (e: any) {
updateSocketInfo(socketInfo, internalOpaque /* , rawError */);
urllibResponse.rt = performanceTime(requestStartTime);
channels.fetchResponse.publish({
fetch: fetchMeta,
error: e,
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export function isReadable(stream: any) {

export function updateSocketInfo(socketInfo: SocketInfo, internalOpaque: any, err?: any) {
const socket = internalOpaque[symbols.kRequestSocket] ?? err?.[symbols.kErrorSocket];

if (socket) {
socketInfo.id = socket[symbols.kSocketId];
socketInfo.handledRequests = socket[symbols.kHandledRequests];
Expand Down
40 changes: 40 additions & 0 deletions test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,44 @@ describe('fetch.test.ts', () => {
assert(stats);
assert(Object.keys(stats).length > 0);
});

it('fetch error should has socket info', async () => {
let requestDiagnosticsMessage: RequestDiagnosticsMessage;
let responseDiagnosticsMessage: ResponseDiagnosticsMessage;
let fetchDiagnosticsMessage: FetchDiagnosticsMessage;
let fetchResponseDiagnosticsMessage: FetchResponseDiagnosticsMessage;
diagnosticsChannel.subscribe('urllib:request', msg => {
requestDiagnosticsMessage = msg as RequestDiagnosticsMessage;
});
diagnosticsChannel.subscribe('urllib:response', msg => {
responseDiagnosticsMessage = msg as ResponseDiagnosticsMessage;
});
diagnosticsChannel.subscribe('urllib:fetch:request', msg => {
fetchDiagnosticsMessage = msg as FetchDiagnosticsMessage;
});
diagnosticsChannel.subscribe('urllib:fetch:response', msg => {
fetchResponseDiagnosticsMessage = msg as FetchResponseDiagnosticsMessage;
});
FetchFactory.setClientOptions({});

try {
await fetch(`${_url}html?timeout=9999`, {
signal: AbortSignal.timeout(100),
});
} catch (error) {
console.log(error);
}
Comment on lines +84 to +86
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling in test

The current error handling simply logs the error without any assertions. Consider adding explicit error type checking and assertions.

    } catch (error) {
-     console.log(error);
+     expect(error).toBeInstanceOf(DOMException);
+     expect(error.name).toBe('TimeoutError');
    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch (error) {
console.log(error);
}
} catch (error) {
expect(error).toBeInstanceOf(DOMException);
expect(error.name).toBe('TimeoutError');
}


assert(requestDiagnosticsMessage!.request);
assert(responseDiagnosticsMessage!.request);
assert(responseDiagnosticsMessage!.response);
assert([ '127.0.0.1', '::1' ].includes(responseDiagnosticsMessage!.response.socket.localAddress));

assert(fetchDiagnosticsMessage!.fetch);
assert(fetchResponseDiagnosticsMessage!.fetch);

const stats = FetchFactory.getDispatcherPoolStats();
assert(stats);
assert(Object.keys(stats).length > 0);
});
});