[listen] improve onError API#29
Conversation
There was a problem hiding this comment.
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.
| static ErrorCallback onError = ( | ||
| Object error, | ||
| StackTrace? stackTrace, { | ||
| ErrorContext? context, | ||
| }) { | ||
| Error.throwWithStackTrace(error, stackTrace ?? StackTrace.current); | ||
| }; |
There was a problem hiding this comment.
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 };
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
[vector_math]///).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-assistbot 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
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