Salamek / huawei-lte-api-ts

API For huawei LAN/WAN LTE Modems written in TypeScript
GNU Lesser General Public License v3.0
48 stars 19 forks source link

Uncatchable error on multible runs #11

Closed zinen closed 3 years ago

zinen commented 3 years ago

Tried running the example code from README.md. But doing so about 5-8 times in a row produces an uncatchable error. The error is thrown by the process "task_queues" and not from inside my the running code.

Tried wrapping the whole code it inside try/catch but that doesn't catch it.

I'm no typescript expert. But can this be fixed to be catchable or can i somehow catch it?

Code:

const huaweiLteApi = require('huawei-lte-api');
const connection = new huaweiLteApi.Connection('http://admin:password@192.168.8.1/');
connection.ready.then(function() {
    console.log('Ready');
    const device = new huaweiLteApi.Device(connection);
    device.signal().then(function(result) {
        console.log(result);
    }).catch(function(error) {
        console.error(error);
    });
});

Error

(node:5980) UnhandledPromiseRejectionWarning: Error: 108003: Already login
    at LoginErrorAlreadyLoginException.ResponseErrorException [as constructor] (.\huawei-lte-api-ts\dist\exceptions.js:22:28)
    at new LoginErrorAlreadyLoginException (.\huawei-lte-api-ts\dist\exceptions.js:81:42)
    at .\huawei-lte-api-ts\dist\api\User.js:118:35
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:5980) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 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(). (rejection id: 1)
(node:5980) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Salamek commented 3 years ago

@zinen try to use catch block on ready, like this:

connection.ready.then(function() {
    console.log('Ready');
    const device = new huaweiLteApi.Device(connection);
    device.signal().then(function(result) {
        console.log(result);
    }).catch(function(error) {
        console.error(error);
    });
}).catch(function(error) {
     console.log('ERROR in ready');
      console.error(error);
 });
zinen commented 3 years ago

Yep, that works. Thanks.

I dont understand why the then.catch works and the try/catch doesn't though.

But I can make this work.