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.

Question: How to recover from an error result to an ok result dynamically?

See original GitHub issue

I see that I can map an ok value to any result I want by using .andThen. I can use unwrapOr to replace every error by given value.

But what about mapping an error value to an ok value dymically? It’s sound quite useful that we want to recover after error and e.g. provide a default value for one type of error and left error as is for other type.

e.g.

const occupationResult: Result<string, NotFoundError | ConnectionError> = await getUserOccupation();

const safeOccupationResult = occupationResult.someErrorMappingFunction(
  (error: NotFoundError | ConnectionError) => {
    if (err instanceof NotFoundError) {
      return ok("User not found");
    } else {
      return error;
    }
  }
);


// comparison to promises:
// a - rejected promise
const a = Promise.reject("an error");
// b - resolved promise
const b = a.catch(err => 33);
// c - rejected promise
const c = a.catch(err => Promise.reject("other error"));

how I could achieve currently the result that my imaginary function someErrorMappingFunction do?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
paduccommented, Jan 12, 2021

I’m not sure I like the idea of having map and mapErr accept callbacks returning Results. The point of map and mapErr is that they do not change the nature of the Result (an Ok remains an Ok, an Err remains an Err), they should only transform the value itself.

Promises are a bit different in that they auto-unwrap (Promise<Promise<T>> becomes a Promise<T>) which is possible because they do not handle the error case.

That’s why I believe Result should have distinct map mapErr andThen and orElse as they all have a very specific transformation purpose. This is not specific to neverthrow, we find the same distinct methods in most (all?) monad libs that have an Either type.

0reactions
supermacrocommented, Jan 13, 2021

orElse is now implemented:

https://github.com/supermacro/neverthrow#resultorelse-method

Closing the issue to keep things a bit tidy, but feel free to chat here or in a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - How to store results of a Dynamic Query in a temp table ...
My answer solves only one problem, storing the result of a dynamic proc call in a temp table. And there are more problems....
Read more >
Dynamic query execution error - DBA Stack Exchange
The error you are receiving is because you have a trailing end; in your dynamic SQL version that is out of place (and...
Read more >
An error occurred when sending commands to the program in ...
Method 3: Reset file associations · Right-click the Excel workbook, point to Open with, and then click More apps. · Select the version...
Read more >
Dynamic Dynamic SQL - Ask TOM
Hi Tom, Looks like I got it figured out why it was not inserting any records.I suplied the values in the wrong order...
Read more >
48 answers on StackOverflow to the most popular Angular ...
If the result of an HTTP request to a server or some other expensive ... To fix this error, you just need to...
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