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.

How to Aggregate Errors on Tasks?

See original GitHub issue

This is a use case I haven’t been able to solve.

I have 3 http requests, if one fails, I want, at least, the result of the other 2. Ideally, I would want to know what error happened.

I’m still using data.task from folktale 1, and this is how I do the requests, which fails fast (if one fails, I only get the first error with no result):

const mergeResults = data => logs => customer => ({ data, logs, customer })

liftA3(
  mergeResults,
  getDataFromAPI(userId),
  getLogs(userId),
  getCustomer(customerID)
).fork(
   error => ... Handle the first thrown error,
   result => ... All 3 requests succeeded here
)

Here, I would like to have data and logs returned if getCustomer API call fails. Thanks in advance

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
robotlolitacommented, Jul 20, 2019

Oh! So you want individual results rather than the overall results. That’s easier.

With:

const Task = require('data.task');
const Validation = require('data.validation');
const { parallel } = require('control.async')(Task);

const toTaskOfValidations = (task) =>
  task.map(Validation.of)
      .orElse(error => Task.of(Validation.Failure(error)));

const runEach = (tasks) =>
  parallel(tasks.map(toTaskOfValidations));

And use it as:

const getDataFromAPI = a => Task.of('data');
const getLogs = b => Task.rejected('logs');
const getCustomer = c => Task.rejected('customer');

const mergeResults = (data, logs, customer) => ({ data, logs, customer });

const userId = 1;

runEach([
  getDataFromAPI(userId),
  getLogs(userId),
  getCustomer(userId)
]).map(xs => mergeResults(...xs))
  .fork(
    error => /* never happens */,
    value => console.log(value)
  );

Here each value of the results record will be a Validation, however, so you’ll need to pattern match on whether each individual result is a failure or a success.

0reactions
diasbrunocommented, Jul 20, 2019

Sorry for suddenly jump in. I’ve recently needed something like this, but with plain promises.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exception handling (Task Parallel Library) - Microsoft Learn
Explore exception handling using the Task Parallel Library (TPL) in .NET. See nested aggregate exceptions, inner exceptions, unobserved task ...
Read more >
AggregateError - David Walsh Blog
The AggregateError error lets developers throw multiple errors within one single Error . Let's see how it works. Website performance monitoring.
Read more >
How to use the Aggregate Function to remove errors in data in ...
How to use the Aggregate Function to remove errors in data in ExcelHow to use the Aggregate Function for data with errors and...
Read more >
Handling exceptions in aggregation flows - IBM
Complete the following tasks: ... If an error is detected downstream of an AggregateReply node, the integration node issues an exception.
Read more >
Aggregate Error Messages In a Calculation Editor
Use a Level of Detail (LOD) Expression to make an aggregation non-aggregate. All LOD expression return non-aggregated values. [Sales]/SUM( [ ...
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