Handling Errors of Function External Module

Hi
i am using node-zklib package
it works but if there any error NR will stop running because of Uncaught Exception
my question is how can i handle all errors of this module to prevent NR form Stop ?

This is a sample of error

1 May 13:46:02 - [red] Uncaught Exception:
1 May 13:46:02 - UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#<ZKError>".

C:\Users\USER>

I assume you are using this module in a function node?

Show us your code & we can advise.

Yes i am using function node

//172.16.12.201
const test = async () => {


    let zkInstance = new ZKLib('172.16.12.201', 4370, 10000, 4000);
    try {
        // Create socket to machine 
        await zkInstance.createSocket()


        // Get general info like logCapacity, user counts, logs count
        // It's really useful to check the status of device 
        node.warn(await zkInstance.getInfo())
    } catch (e) {
        node.warn(e)
        if (e.code === 'EADDRINUSE') {
        }
    }


    // Get users in machine 
    const users = await zkInstance.getUsers()
    node.warn(users)


    // Get all logs in the machine 
    // Currently, there is no filter to take data, it just takes all !!
    const logs = await zkInstance.getAttendances()
    node.warn(logs)


    const attendances = await zkInstance.getAttendances((percent, total) => {
        // this callbacks take params is the percent of data downloaded and total data need to download 
    })

    // YOu can also read realtime log by getRealTimelogs function

    // node.warn('check users', users)

    zkInstance.getRealTimeLogs((data) => {
        // do something when some checkin 
        node.warn(data)
    })



    // delete the data in machine
    // You should do this when there are too many data in the machine, this issue can slow down machine 
    

    // Get the device time
    //const getTime = await zkInstance.getTime();
    //node.warn(getTime.toString());

    // Disconnect the machine ( don't do this when you need realtime update :))) 
    await zkInstance.disconnect()

}

test()

return msg;

Please note that the main topic is how to handle the error to prevent stopping NR not how solve the error

Everywhere you have an await you should be surrounded by try catch (or wrap the whole inner of the function in a try catch

  try {
    await zkInstance.disconnect()
  } catch(e) {
    node.error(e, msg);
    return;
  }

I try it but same result error can't be handled

1 May 14:42:22 - [red] Uncaught Exception:
1 May 14:42:22 - UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#<ZKError>".

C:\Users\USER>

Please post your code as it is now

Sorry i didn't do exactly what you said
Now the error is handled
But the content of error can't be displayed

{"message":"[object Object]","source":{"id":"0b8889d8e8629a19","type":"function","name":"zklib","count":1}}

Here is my code

//172.16.12.201
const test = async () => {


    let zkInstance = new ZKLib('172.16.12.201', 4370, 10000, 4000);
    try {
        // Create socket to machine 
        await zkInstance.createSocket()


        // Get general info like logCapacity, user counts, logs count
        // It's really useful to check the status of device 
        node.warn(await zkInstance.getInfo())
    } catch (e) {
        node.error(e, msg);
        node.warn(e)
        if (e.code === 'EADDRINUSE') {
        }
    }


    // Get users in machine 

    
    try {
    const users = await zkInstance.getUsers()
    node.warn(users)

  } catch(e) {
    node.error(e, msg);
    return;
  }

    


    // Get all logs in the machine 
    // Currently, there is no filter to take data, it just takes all !!
    
    const logs = await zkInstance.getAttendances()
    node.warn(logs)

    try {
       const attendances = await zkInstance.getAttendances((percent, total) => {
        // this callbacks take params is the percent of data downloaded and total data need to download 
    })
  } catch(e) {
    node.error(e, msg);
    return;
  }


    // YOu can also read realtime log by getRealTimelogs function

    // node.warn('check users', users)
/*
    zkInstance.getRealTimeLogs((data) => {
        // do something when some checkin 
        node.warn(data)
    })

*/

    // delete the data in machine
    // You should do this when there are too many data in the machine, this issue can slow down machine 
    

    // Get the device time
    //const getTime = await zkInstance.getTime();
    //node.warn(getTime.toString());

    // Disconnect the machine ( don't do this when you need realtime update :))) 
   


    try {
    await zkInstance.disconnect()
  } catch(e) {
    node.error(e, msg);
    return;
  }

}

test()

return msg;

Is that from the warn or the error?

Put console.error(e); below node.error(e, msg);, & check the console - what do you see?
Change node.warn(e) to node.warn({error: e});, what do you see?

Hi
after period of success of handling error
now i have some unhandled error

8 May 11:32:56 - [red] Uncaught Exception:
8 May 11:32:56 - TypeError: Cannot read property 'removeListener' of null
    at internalCallback (C:\Users\USER\.node-red\node_modules\node-zklib\zklibtcp.js:116:21)
    at Timeout._onTimeout (C:\Users\USER\.node-red\node_modules\node-zklib\zklibtcp.js:129:13)
    at listOnTimeout (node:internal/timers:555:17)
    at processTimers (node:internal/timers:498:7)

This is my code even i catch all the possible error

//172.16.12.201
const test = async () => {


  let zkInstance = new ZKLib('172.16.12.201', 4370, 10000);
  try {
    // Create socket to machine 
    await zkInstance.createSocket()


    // Get general info like logCapacity, user counts, logs count
    // It's really useful to check the status of device 
    node.warn(await zkInstance.getInfo())
  } catch (e) {

    node.warn(["Create socket", { "error": e }])
    node.error(e, msg);
    console.error(e)

    if (e.code === 'EADDRINUSE') {
    }
  }




  // Get users in machine 


  try {
    var users = await zkInstance.getUsers()
    node.warn(users)

  } catch (e) {
    node.warn(["getUsers", { "error": e }])
    node.error(e, msg);
    console.error(e)
    return;
  }



  // Get all logs in the machine 
  // Currently, there is no filter to take data, it just takes all !!



  try {
    var logs = await zkInstance.getAttendances()
    

    for (let log_list = 0; log_list < logs.data.length; log_list++) {
      let newDate=new Date(logs.data[log_list].recordTime).toLocaleString("en-GB")
      logs.data[log_list].recordTime = newDate.slice(0, 10) + newDate.slice(11);//remove comma 
      for (let usr_list = 0; usr_list < users.data.length; usr_list++) {
        if (users.data[usr_list].userId == logs.data[log_list].deviceUserId) {
          
          logs.data[log_list].name = users.data[usr_list].name
        }
      }



    }
    node.warn(logs)
    
    
    


  } catch (e) {
    node.warn(["getAttendances", { "error": e }])
    node.error(e, msg);
    console.error(e)
    return;
  }





  try {
    const getTime = await zkInstance.getTime()
    node.warn(getTime.toLocaleString("en-GB")
    );
    
  } catch (e) {
    node.warn(["getTime", { "error": e }])
    node.error(e, msg);
    console.error(e)
    return;
  }

   

  try {
    node.warn(["Read saved data", await zkInstance.executeCmd(11)])



  } catch (e) {
    node.warn(["Read saved data", { "error": e }])
    node.error(e, msg);
    console.error(e)
    return;
  }


   try {
    await zkInstance.freeData()
    
    
    
  } catch (e) {
     node.warn(["freeData()", { "error": e }])
    node.error(e, msg);
    console.error(e)
    return;
  }



  try {
    await zkInstance.disconnect()
  } catch (e) {
    node.warn(["disconnect", { "error": e }])
    node.error(e, msg);
    console.error(e)
    return;
  }

  return logs;

}




msg=test()

return msg;

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.