Skip to content

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

@jiangcuo

Description

@jiangcuo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions