#Unable to catch errors

24 messages · Page 1 of 1 (latest)

runic swan
#

I am using Electron net request module to send requests. Sometimes requests might fail for various reasons and they pop an error in the app. For this reason i used uncaughtException handler to catch them. I need somehow to do try{}catch() on top of this request handle and to be able to catch the request because this request is being executed in an async function and i cant afford it to become a zombie or just stop working. I need to know when some async function stops working because of this and which one it is. How can i possibly catch this error because try catching this request doesnt work?

I simply send the request inside

async function someFunction(){
.
.
.
var getResult = await sendRequest()... //need to catch it here 
.
.
}
#
const request = net.request(
            {
                method: type,
                url: url,
                headers: headers,
            });
        request.on('login', (authInfo, callback) => {
            
        });

        request.on('response', (response) => {
             response.on('error', (error) => {
                resolve({status: false});
            })
            response.on('data', (chunk) => {
                
                
            })
            response.on('end', () => {
            })
        });
#

This is how sendRequest function looks like mainly

final barn
#

request.on('error'

runic swan
#

thanks!

#

@final barn

runic swan
#

@final barn

#
    at new NodeError (node:internal/errors:399:5)
    at Socket._writeGeneric (node:net:920:8)
    at Socket._write (node:net:942:8)
    at doWrite (node:internal/streams/writable:411:12)
    at clearBuffer (node:internal/streams/writable:572:7)
    at Writable.uncork (node:internal/streams/writable:351:7)
    at ClientRequest._flushOutput (node:_http_outgoing:1133:10)
    at ClientRequest._flush (node:_http_outgoing:1102:22)
    at onSocketNT (node:_http_client:906:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'ERR_SOCKET_CLOSED'```
#

this is error im not catching

#

with this

#

do you know why?

final barn
#

no

runic swan
#

do you have any idea? @final barn

#

request.on('error' doesnt handle it

final barn
runic swan
#

ty

ashen steeple
#

You need to use reject(error) in the error case to if you want to be able to try await catch something that you converted into a promise

runic swan
#

such as err_socket_close

ashen steeple
#

Ok. Because in on "error" you have a resolve instead of a reject in your code

runic swan
#

im catching that on the other side on await

ashen steeple
#

Only if you want to catch that with try catch