question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access context after run

See original GitHub issue

Some 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:closed
  • Created 2 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
cenk1cenk2commented, Jun 7, 2021

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.

const tasks = new Listr({})

try {
  await tasks.run()
} catch {
  // as in the tasks.err
  await tasks.ctx.closeConnection()
}

With the current error structure you can have the last error therefore the context as follows:

const tasks = new Listr({})

try {
  await tasks.run()
} catch {
  // as in the tasks.err
  await tasks.err.pop().context.closeConnection()
}

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found