Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors not captured in methods? #809

Closed
jaidetree opened this issue Jun 28, 2024 · 4 comments
Closed

Errors not captured in methods? #809

jaidetree opened this issue Jun 28, 2024 · 4 comments

Comments

@jaidetree
Copy link

Given code like the following:

Bacon
 .once(null)
 .map((_x) => {
   throw new Error("Expected failure")
 }) 
 .toPromise()
 .then(x => {
  console.log("Promise was unexpectedly resolved")
 })
 .catch(x => {
   if (x instanceof Error && x.message.includes("Expected failure")) {
     console.log("Handled the expected failure")
   } else {
     console.log("Something went wrong")
   }
 })

I expect the error in the map operator to make its way through the promise, then the catch handler can test the error and log my expected message.

Instead, the stream exits immediately and it seems like an error is thrown outside of the stream.

Is the idea that I should be wrapping my logic in a try...catch clause or is this an unexpected bug?

@raimohanska
Copy link
Contributor

It’s intended - Bacon has never caught exceptions. The thinking is that exceptions are treated as programming errors and should fail fast. Which is ofc opinionated.

@jaidetree
Copy link
Author

Thanks for the insight, seems reasonable

@semmel
Copy link
Member

semmel commented Jun 30, 2024

@jaidetree Otherwise you can use Bacon.try together with .flatMap like this:

wrapped = source.flatMap(Bacon.try(dangerousOperation))

@jaidetree
Copy link
Author

That's perfect thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants