othiym23 / node-continuation-local-storage

implementation of https://github.com/joyent/node/issues/5243
BSD 2-Clause "Simplified" License
1.13k stars 207 forks source link

TypeScript ES6 async/await generator and an error thrown in a native Promise #131

Open mweathers opened 6 years ago

mweathers commented 6 years ago

I've run into an issue when throwing an error from a promise chain (even if the error is caught and handled) that is wrapped in TypeScript's async/await generator. It seems as though new contexts inherit from the context that had the error thrown. I've written some example TS code that demonstrates this, as well as the JS output by the TS compiler.

TypeScript

const cls = require('continuation-local-storage');

const ns = cls.createNamespace('my ns');

ns.run(async () => {
    ns.set('key', 'value');
    await Promise.resolve().then(() => {
        console.log({ key1: ns.get('key') });
        throw new Error('test');
    }).catch(() => {});
});

setTimeout(() => {
    ns.runAndReturn(() => {
        console.log({ key2: ns.get('key') });
    });
}, 1000);

JS:

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
const cls = require('continuation-local-storage');
const ns = cls.createNamespace('my ns');
ns.run(() => __awaiter(this, void 0, void 0, function* () {
    ns.set('key', 'value');
    yield Promise.resolve().then(() => {
        console.log({ key1: ns.get('key') });
        throw new Error('test');
    }).catch(() => { });
}));
setTimeout(() => {
    ns.runAndReturn(() => {
        console.log({ key2: ns.get('key') });
    });
}, 1000);

Expected Output:

{ key1: 'value' }
{ key2: undefined }

Actual Output:

{ key1: 'value' }
{ key2: 'value' }
nishch commented 6 years ago

I also observed the same problem, however it's not related to generators:


const ns = cls.createNamespace("namespace1");

ns.run(() => {
    ns.set("name", "Nishant");

    setTimeout(() => {
        Promise.resolve()
            .then(() => {
                for (var i = 0; i < 10; i++) {
                    if (i === 8) {
                        console.log(ns.get("name"));
                        throw new Error("causing trouble");
                    }
                }
            }).catch(err => {
                console.log("catching the errors");
            });
    });
}, 1000);

setTimeout(() => {
    console.log(ns.get("name"));
}, 2000);

Expected output:

catching the errors
undefined

Actual output:


catching the errors
Nishant```
0Ams commented 3 years ago

I tested it, but the issue did not occur. using node v14.16.1, ts 4.2.3

    const ns = cls.getNamespace('app');
    ns.run(async () => {
      ns.set('key', 'value');
      await Promise.resolve()
        .then(() => {
          console.log({ key1: ns.get('key') });
          throw new Error('test');
        })
        .catch(e => {
          console.log(e);
        });
    });

    setTimeout(() => {
      ns.runAndReturn(() => {
        console.log({ key2: ns.get('key') });
      });
    }, 100);
    await Bluebird.delay(1000);

output:

{ key1: 'value' }
Error: test
{ key2: 'value' }