Skip to content

[listen] improve onError API#29

Open
chunhtai wants to merge 2 commits into
flutter:mainfrom
chunhtai:improve-error
Open

[listen] improve onError API#29
chunhtai wants to merge 2 commits into
flutter:mainfrom
chunhtai:improve-error

Conversation

@chunhtai

Copy link
Copy Markdown
Contributor

flutter/flutter treats different error differently.

For stateError when using changenotifier, it throws synchronously

for Error raise during listener callback, it uses reportError and continue execution.

Improve the Error API so that framework can tell where the error is raised

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2

@github-actions github-actions Bot added triage-framework Should be looked at in framework triage p: listen labels Jul 10, 2026
@chunhtai chunhtai requested a review from loic-sharma July 10, 2026 03:37

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates ErrorCallback to pass the raw error object and original StackTrace, and introduces an optional ErrorContext parameter to distinguish listener errors from assertion errors. The default onError implementation is updated to rethrow errors preserving their stack trace. Review feedback suggests fallback logic to preserve the stack trace of Error objects when the provided stack trace is null, and recommends using list assertions in tests instead of .single to improve test failure diagnostics.

Comment thread packages/listen/lib/src/listen.dart Outdated
Comment on lines 77 to 83
static ErrorCallback onError = (
Object error,
StackTrace? stackTrace, {
ErrorContext? context,
}) {
Error.throwWithStackTrace(error, stackTrace ?? StackTrace.current);
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

When stackTrace is null, we can try to preserve the original stack trace of the error if it is an Error object (which has a stackTrace getter) before falling back to StackTrace.current. This helps preserve the original context of where the error was created.

  static ErrorCallback onError = (\n    Object error,\n    StackTrace? stackTrace, {\n    ErrorContext? context,\n  }) {\n    Error.throwWithStackTrace(\n      error,\n      stackTrace ?? (error is Error ? error.stackTrace : null) ?? StackTrace.current,\n    );\n  };

Comment thread packages/listen/test/listen_test.dart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: listen triage-framework Should be looked at in framework triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants