Skip to content

Usage in prod - service logic should not rely on success of ns.run #45

Open
@orishofman

Description

Hi,
I want to use cls-hooked in my services that run in a prod env.
From what I saw in the Namespace.prototype.run source code, the callback I set to run when ns.run is finished will run only if the code in the try statement will not throw any errors.
This means that if by any chance there will be an error before my callback will be called, then my code won't run.

I am using this package in a way I believe many others are using it(I am running my service with express) - calling "ns.run(() =>{ next() })" in a middleware in the start of the chain and then use the namespace in later parts of the chain. I am using it to add a transaction id to all my logs.
If the call to ns.run would fail for any reason, I would of course prefer my code to run anyway and to just not have the transaction id in the logs of this specific session.
From what I understand from the code I read, if ns.run will fail then my code won't run for the specific api request.

I guess that if there will be a "ns.safeRun" or something of the kind that will act the same as ns.run but in the end will run my callback either way then this would solve my issue.
I believe that this is something that other people who will use this package will want.

Or, maybe there is a way to do what I wrote here and I just didn't figure it out.
What do you say?

Thank you!

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions