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

fix(core): improve error handling with multiple errors in flashCallbacks #3011

Conversation

nightohl
Copy link
Contributor

@nightohl nightohl commented Mar 2, 2025

Related Bug Reports or Discussions

Fixes #

Summary

Previously, only the first error was thrown and subsequent errors were ignored. Now all errors are collected and:

  • If there's only one error, throw it directly
  • If there are multiple errors, throw them as AggregateError

This change improves debugging by preserving all error information during callback execution.

Check List

  • pnpm run fix for formatting and linting code and docs

Copy link

vercel bot commented Mar 2, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 10, 2025 0:30am

Copy link

codesandbox-ci bot commented Mar 2, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@dai-shi
Copy link
Member

dai-shi commented Mar 2, 2025

@dmaskasky What do you think?

Copy link

pkg-pr-new bot commented Mar 2, 2025

Open in Stackblitz

More templates

npm i https://pkg.pr.new/jotai@3011

commit: d873d87

Copy link

github-actions bot commented Mar 2, 2025

LiveCodes Preview in LiveCodes

Latest commit: d873d87
Last updated: Mar 10, 2025 12:29pm (UTC)

Playground Link
React demo https://livecodes.io?x=id/DUVV6M8JJ

See documentations for usage instructions.

Comment on lines 489 to 496

if (errors.length === 1) {
throw errors[0]
} else if (errors.length > 1) {
throw new AggregateError(
errors,
'Multiple errors occurred during flushCallbacks',
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would even go with this. Not sure about the drawback, but it's predictable.

Suggested change
if (errors.length === 1) {
throw errors[0]
} else if (errors.length > 1) {
throw new AggregateError(
errors,
'Multiple errors occurred during flushCallbacks',
)
if (errors.length) {
throw new AggregateError(errors)

Copy link
Contributor Author

@nightohl nightohl Mar 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai-shi I think it's good way to go!
I'd change commit with it and force-push, thanks!

  • done

Previously, only the first error was thrown and subsequent errors were ignored.
Now all errors are collected and:
- If there's only one error, throw it directly
- If there are multiple errors, throw them as AggregateError

This change improves debugging by preserving all error information during callback execution.

Co-authored-by: daishi <[email protected]>
@nightohl nightohl force-pushed the fix-when-multiple-errors-occured-in-flushCallbacks branch from 6e45368 to 310a58b Compare March 2, 2025 03:13
@dmaskasky
Copy link
Collaborator

@dmaskasky What do you think?

My main concern is that AggregateError will not pass an instance check.

try {
  store.set(someAtom, ...values)
} catch(e) {
  if (e extends FooError) {
    // handle error
  }
}

Are there examples where AggregateError is used in other open source libraries?

@dmaskasky
Copy link
Collaborator

Reading more on AggregateError, I think this makes sense.

Could we do

if (errors.length === 1) {
  throw errors[0]
} else if (errors.length) {
  throw new AggregateError(errors) 
}

@dai-shi
Copy link
Member

dai-shi commented Mar 2, 2025

My main concern is that AggregateError will not pass an instance check.

I don't think it's solved anyways, because you don't know if other subscribers throw an error.

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this all-or-nothing approach. It's more predictable.

@dai-shi dai-shi changed the title feat(flushCallbacks): improve error handling with multiple errors fix(core): improve error handling with multiple errors in flashCallbacks Mar 2, 2025
@dmaskasky
Copy link
Collaborator

I don't think it's solved anyways, because you don't know if other subscribers throw an error.

Should we wrap all errors in AggregateError? Is this something we should teach?

@dai-shi
Copy link
Member

dai-shi commented Mar 2, 2025

Should we wrap all errors in AggregateError? Is this something we should teach?

Yes, that's exactly my suggestion. It's harder to tell when it's wrapped otherwise.

@dai-shi dai-shi added this to the v2.12.2 milestone Mar 10, 2025
@dai-shi dai-shi merged commit d06e958 into pmndrs:main Mar 10, 2025
44 checks passed
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

Successfully merging this pull request may close these issues.

3 participants