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

Missing code property on error #1865

Closed
rightaway opened this issue Sep 9, 2021 · 15 comments
Closed

Missing code property on error #1865

rightaway opened this issue Sep 9, 2021 · 15 comments

Comments

@rightaway
Copy link

I'm getting no code property on the error.

RequestError: connect ECONNREFUSED 127.0.0.1:123,
  at ClientRequest.<anonymous> (/proj/node_modules/got/dist/source/core/index.js:953:111),
  at Object.onceWrapper (events.js:520:26),
  at ClientRequest.emit (events.js:412:35),
  at ClientRequest.origin.emit (/proj/node_modules/@szmarczak/http-timer/dist/source/index.js:39:20),
  at onerror (/proj/node_modules/agent-base/dist/src/index.js:117:21),
  at callbackError (/proj/node_modules/agent-base/dist/src/index.js:136:17),
  at processTicksAndRejections (internal/process/task_queues.js:95:5),
  at SocksClient.closeSocket (/proj/node_modules/socks/build/client/socksclient.js:387:32),
  at SocksClient.onErrorHandler (/proj/node_modules/socks/build/client/socksclient.js:360:14),
  at Socket.onError (/proj/node_modules/socks/build/client/socksclient.js:222:38),

error.code is undefined but it is a got Error because error instanceof RequestError is true. Could it be because of socksclient.js?

How can I get access to error.code which should be ECONNREFUSED here?

@szmarczak
Copy link
Collaborator

Can you please fill in the issue template properly? How to reproduce this?

@rightaway
Copy link
Author

Using node 14.17.4, got 11.8.1 and socks-proxy-agent 5.0.0.

When using a proxy the timeout isn't used, and code is undefined. (Don't know how to duplicate ECONNREFUSED from the issue)

const got = require("got")
const { SocksProxyAgent } = require("socks-proxy-agent")

// Error right away ("ETIMEDOUT", "Timeout awaiting "request" for 1ms")
got("https://github.com", { retry: 0, timeout: 1 })
  .catch(e => console.log(e.code, e.message))

// Error after 30 seconds ("undefined", "Proxy connection timed out")
got("https://github.com", { retry: 0, timeout: 1, agent: { https: new SocksProxyAgent("socks5://127:0.0.1:123") } })
  .catch(e => console.log(e.code, e.message))

There are other inconsistencies with using a proxy or not. For example using a wrong domain (github.comm)

// Error right away ("ETIMEDOUT", "Timeout awaiting "request" for 1ms")
got("https://github.comm", { retry: 0, timeout: 1 })
  .catch(e => console.log(e.code, e.message))

// Error right away ("ENOTFOUND", "getaddrinfo ENOTFOUND github.comm")
got("https://github.comm", { retry: 0, timeout: 1, agent: { https: new SocksProxyAgent("socks5://127:0.0.1:123") } })
  .catch(e => console.log(e.code, e.message))

With no timeout they are the same

// Error right away ("ENOTFOUND", "getaddrinfo ENOTFOUND github.comm")
got("https://github.comm", { retry: 0 })
  .catch(e => console.log(e.code, e.message))

// Error right away ("ENOTFOUND", "getaddrinfo ENOTFOUND github.comm")
got("https://github.comm", { retry: 0, agent: { https: new SocksProxyAgent("socks5://127:0.0.1:123") } })
  .catch(e => console.log(e.code, e.message))

@szmarczak
Copy link
Collaborator

The most probable thing is that the upstream error doesn't have .code property defined. Can you try the beta version please?

@rightaway
Copy link
Author

Tried 12.0.0-beta.4 but same behavior.

Shouldn't the timeout be used even if we're using a proxy? And same error messages with and without proxy?

@szmarczak
Copy link
Collaborator

Shouldn't the timeout be used even if we're using a proxy? And same error messages with and without proxy?

Agents may implement their own timeouts, which aren't compatible with Got.

Tried 12.0.0-beta.4 but same behavior.

Unfortunately without reproducible code we can't do much :(

@rightaway
Copy link
Author

Agents may implement their own timeouts, which aren't compatible with Got.

socks takes a timeout option so we can pass the same timeout we pass to got to the Agent, but it's not really the same thing. That timeout is just for the Agent, then there could be more time spent for the rest of the request, so the request can take longer than the timeout but doesn't fail.

Since got is calling Agent, can't it kill the request if the total time taken by everything exceeds the timeout?

Is there any way to get access to the underlying Error object thrown by the Agent? Could something like a cause property (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#differentiate_between_similar_errors) be added to RequestError with the original Error object so that we can check that underlying error and respond accordingly?

@szmarczak
Copy link
Collaborator

can't it kill the request if the total time taken by everything exceeds the timeout?

The socket belongs to Agent, Got doesn't control it, so no.

@szmarczak
Copy link
Collaborator

cause is supported on Node.js 16+

@rightaway
Copy link
Author

cause is just an example, it could be any property on the got Error object like original that stores the original Error if there was one. It would really help with more involved error handling scenarios.

@Koyamie
Copy link

Koyamie commented Oct 13, 2021

I am facing the same issue, any error that got returns are sent without the code property.

@szmarczak
Copy link
Collaborator

@Koyamie any reproducible code please?

@Koyamie
Copy link

Koyamie commented Dec 10, 2021

@Koyamie any reproducible code please?

It has been fixed in a recent commit, can't tell which but I am no longer facing this issue.

@rightaway
Copy link
Author

@szmarczak Could you add a property on the got Error object like original that stores the original Error if there was one for example from socks-proxy-agent? It would really help with more involved error handling scenarios.

@szmarczak
Copy link
Collaborator

Can you open another issue please?

@rightaway
Copy link
Author

@szmarczak Can you please open this issue? I don't know why @Koyamie thinks this is fixed in a recent commit.

Reproducible code for one of the cases is #1865 (comment). Don't know how to reproduce ECONNREFUSED.

More examples

RequestError: connect ECONNREFUSED 127.0.0.1:123,
  at ClientRequest.<anonymous> (/proj/node_modules/got/dist/source/core/index.js:953:111),
  at Object.onceWrapper (events.js:520:26),
  at ClientRequest.emit (events.js:412:35),
  at ClientRequest.origin.emit (/proj/node_modules/@szmarczak/http-timer/dist/source/index.js:39:20),
  at onerror (/proj/node_modules/agent-base/dist/src/index.js:117:21),
  at callbackError (/proj/node_modules/agent-base/dist/src/index.js:136:17),
  at runMicrotasks (<anonymous>),
  at processTicksAndRejections (internal/process/task_queues.js:95:5),
  at SocksClient.closeSocket (/proj/node_modules/socks/build/client/socksclient.js:387:32),
  at SocksClient.onErrorHandler (/proj/node_modules/socks/build/client/socksclient.js:360:14),
  at Socket.onError (/proj/node_modules/socks/build/client/socksclient.js:222:38),
  at Object.onceWrapper (events.js:520:26),
  at Socket.emit (events.js:400:28),
  at emitErrorNT (internal/streams/destroy.js:106:8),
  at emitErrorCloseNT (internal/streams/destroy.js:74:3),
  at processTicksAndRejections (internal/process/task_queues.js:82:21)
RequestError: read ECONNRESET,
  at ClientRequest.<anonymous> (/proj/node_modules/got/dist/source/core/index.js:953:111),
  at Object.onceWrapper (events.js:520:26),
  at ClientRequest.emit (events.js:412:35),
  at ClientRequest.origin.emit (/proj/node_modules/@szmarczak/http-timer/dist/source/index.js:39:20),
  at onerror (/proj/node_modules/agent-base/dist/src/index.js:117:21),
  at callbackError (/proj/node_modules/agent-base/dist/src/index.js:136:17),
  at runMicrotasks (<anonymous>),
  at processTicksAndRejections (internal/process/task_queues.js:95:5),
  at SocksClient.closeSocket (/proj/node_modules/socks/build/client/socksclient.js:387:32),
  at SocksClient.onErrorHandler (/proj/node_modules/socks/build/client/socksclient.js:360:14),
  at Socket.onError (/proj/node_modules/socks/build/client/socksclient.js:222:38),
  at Object.onceWrapper (events.js:520:26),
  at Socket.emit (events.js:400:28),
  at emitErrorNT (internal/streams/destroy.js:106:8),
  at emitErrorCloseNT (internal/streams/destroy.js:74:3),
  at processTicksAndRejections (internal/process/task_queues.js:82:21)
RequestError: Socks5 proxy rejected connection - TTLExpired,
  at ClientRequest.<anonymous> (/proj/node_modules/got/dist/source/core/index.js:953:111),
  at Object.onceWrapper (node:events:510:26),
  at ClientRequest.emit (node:events:402:35),
  at ClientRequest.origin.emit (/proj/node_modules/@szmarczak/http-timer/dist/source/index.js:39:20),
  at onerror (/proj/node_modules/agent-base/dist/src/index.js:117:21),
  at callbackError (/proj/node_modules/agent-base/dist/src/index.js:136:17),
  at runMicrotasks (<anonymous>),
  at processTicksAndRejections (node:internal/process/task_queues:96:5),
  at SocksClient.closeSocket (/proj/node_modules/socks/build/client/socksclient.js:387:32),
  at SocksClient.handleSocks5FinalHandshakeResponse (/proj/node_modules/socks/build/client/socksclient.js:633:18),
  at SocksClient.processData (/proj/node_modules/socks/build/client/socksclient.js:331:22),
  at SocksClient.onDataReceivedHandler (/proj/node_modules/socks/build/client/socksclient.js:304:14),
  at Socket.onDataReceived (/proj/node_modules/socks/build/client/socksclient.js:220:46),
  at Socket.emit (node:events:390:28),
  at addChunk (node:internal/streams/readable:315:12),
  at readableAddChunk (node:internal/streams/readable:289:9),
  at Socket.Readable.push (node:internal/streams/readable:228:10),
  at TCP.onStreamRead (node:internal/stream_base_commons:199:23)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants