Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the socket in case of SocketError #1041

Merged
merged 5 commits into from
Sep 21, 2021
Merged

Expose the socket in case of SocketError #1041

merged 5 commits into from
Sep 21, 2021

Conversation

delvedor
Copy link
Member

If a SocketError happens, a user might want to dig deeper to understand why it has happened. For example, they might want to read the socket local/remote address of the socket.
This pr adds a .socket property to the SocketError which holds the socket that caused the error.

@delvedor delvedor added enhancement New feature or request Client Features or fixes related to the `Client` class labels Sep 21, 2021
@delvedor
Copy link
Member Author

I'm not sure how to add the socket info in lib/api/api-upgrade.js and lib/api/api-connect.js.

@ronag
Copy link
Member

ronag commented Sep 21, 2021

I'm +/- 0. I would like to avoid exposing internals as much as possible so that in the future we could e.g. replace the socket implementation without breaking everything.

Rather than exposing the socket directly could you create a new object that exposes the properties that you need?

@delvedor
Copy link
Member Author

replace the socket implementation without breaking everything.

I don't see the problem, the .socket property will be the different socket implementation that the user is aware of.

Rather than exposing the socket directly could you create a new object that exposes the properties that you need?

I fear this approach might not be future-proof. Today I need the local/remote address, tomorrow might be something else, which will require another pr to adds the new properties (and so on...).

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Adding a reference to the socket might delay the time a socket is garbage collected. This is likely not a problem here as the fd has already been destroyed when this error is called, but it might happen in some other cases if we keep following this pattern elsewhere.

Ragarding @ronag concern about future support for different kind of Socket object... I don't think that would come with anything less than a semver-major change.

I'm ok in shipping this.

types/errors.d.ts Outdated Show resolved Hide resolved
lib/core/errors.js Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@delvedor
Copy link
Member Author

There is still #1041 (comment) that needs to be addressed.

@mcollina
Copy link
Member

@delvedor you mean https://github.com/nodejs/undici/blob/main/lib/api/api-connect.js#L43? I think you'd need to pass the socket to onHeaders.

types/errors.d.ts Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Sep 21, 2021

Codecov Report

Merging #1041 (1f2d877) into main (1e1a6db) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1041   +/-   ##
=======================================
  Coverage   94.89%   94.89%           
=======================================
  Files          37       37           
  Lines        3623     3624    +1     
=======================================
+ Hits         3438     3439    +1     
  Misses        185      185           
Impacted Files Coverage Δ
lib/api/api-connect.js 100.00% <100.00%> (ø)
lib/api/api-upgrade.js 100.00% <100.00%> (ø)
lib/client.js 97.95% <100.00%> (ø)
lib/core/errors.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1e1a6db...1f2d877. Read the comment docs.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina
Copy link
Member

@ronag any final objection

@ronag ronag merged commit ff8ba39 into main Sep 21, 2021
@delvedor delvedor deleted the add-socket-info branch September 22, 2021 05:50
KhafraDev pushed a commit to KhafraDev/undici that referenced this pull request Jun 23, 2022
* Expose the socket in case of SocketError

* Updated test

* Add missing socket

* Address feedback

* Make .socket nullable
crysmags pushed a commit to crysmags/undici that referenced this pull request Feb 27, 2024
* Expose the socket in case of SocketError

* Updated test

* Add missing socket

* Address feedback

* Make .socket nullable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client Features or fixes related to the `Client` class enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants