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

There is no explicit handling for errors that might occur on the request (req) object itself #13

Open
1 task
jiangcuo opened this issue Dec 25, 2024 · 0 comments

Comments

@jiangcuo
Copy link

What happened?

Error reported as follows

node:events:489
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 10.10.10.54:8006
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1570:16)
Emitted 'error' event on ClientRequest instance at:
    at TLSSocket.socketErrorListener (node:_http_client:495:9)
    at TLSSocket.emit (node:events:511:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '10.10.10.54',
  port: 8006
}

In the code below does not handle the req error

let req = this.#http.request(options, (response) => {
response.setEncoding("utf8");
let chunks = "";
response.on("data", (chunk) => {
chunks += chunk;
});
response.on("end", () => {
let data = null;
if (ref.responseType == ResponseType.JSON) {
data = JSON.parse(chunks);
} else if (ref.responseType == ResponseType.PNG) {
data = "data:image/png;base64," + chunks;
}
const result = new Result(
data,
response.statusCode,
response.statusMessage,
resource,
parameters,
response.method,
ref.responseType
);
ref.#lastResult = result;
//debug
this.#log(result.toString());
this.#log(result.response);
resolve(result);
});
response.on("error", (error) => {
this.#error(error);
reject(error);
});
});
if (body != "") {
req.write(body);
}
req.end();

We can add an error event listener on the req object to handle potential request errors.

req.on('error', (error) => {
  this.#error(error);  
  reject(error);     
});

This way we can use catch to handle errors in our own code

Expected behavior

Please evaluate it.

Relevant log output

No response

Proxmox VE Version

latest

Version (bug)

latest

Version (working)

No response

On what operating system are you experiencing the issue?

Windows, Mac OSX, Linux, Other

Pull Request

  • I would like to do a Pull Request
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

1 participant