Access context after run
See original GitHub issueSome of my tasks perform operations, like connecting to a database, and that db connection is stored on the context
.
Regardless of if the pipeline succeeds or fails, whenever it’s all finished, I need to close these connections.
I know that if run succeeds , ctx
will be the first argument listr.run((ctx) => { ctx.db.close(); }
would work fine. But how do you handle the case where it fails? listr.run().catch((err) => { // how to get ctx here?
Ideally for my case, it would be available on the listr
object itself, so I could use it in finally
(although, I would also settle for something like listr.run(closeConn).catch(closeConn)
I was expecting this to work, but it looks like listr.options.ctx
is undefined
const listr = new Listr([
{
task: async (ctx) => {
ctx.test = 'test';
throw new Error('force error');
},
},
]);
listr.run().finally(() => {
console.log('options.ctx', listr.options?.ctx);
});
Any ideas?
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Contexts - GitHub Docs
Contexts are a way to access information about workflow runs, runner environments, jobs, and steps. Each context is an object that contains properties, ......
Read more >How to access Spring context in jUnit tests annotated with ...
How to access Spring context in jUnit tests annotated with @RunWith and @ContextConfiguration? - Stack Overflow. Stack Overflow for Teams – ...
Read more >Which Context Do Flows Run In?
For a flow that runs in system context, the flow access is determined by whether the flow runs in system context with sharing...
Read more >Using Contexts
Contexts provide a mechanism for securing and sharing environment variables across projects. The environment variables are defined as name/value pairs and ...
Read more >Access Context Manager Overview
Access Context Manager allows Google Cloud organization administrators to define fine-grained, attribute based access control for projects and resources in ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
https://listr2.kilic.dev/getting-started/the-concept-of-context#as-class-object
The errors collected have a snapshot of the context object, which can be seen here. But it is an array since not-failing errors are also collected.
If we add a similar approach it would be like this, which is not much different than taking the context outside. But I see your point, it does not make much of a sense since it should be an internal variable, I completely agree with that.
With the current error structure you can have the last error therefore the context as follows: