fix(button): error occurs with in Button click handler cannot be caught#5834
fix(button): error occurs with in Button click handler cannot be caught#5834
Conversation
…ght by vue onErrorCaptured hook
commit: |
📝 WalkthroughWalkthroughThe changes modify the Button component to properly handle asynchronous errors in click event handlers by wrapping the click handler with Vue's Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip You can customize the tone of the review comments and chat replies.Configure the |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
test/components/Button.spec.ts (1)
50-76: Good test coverage for the error handling fix.The test correctly verifies that async errors from click handlers are captured by Vue's
onErrorCapturedhook. A couple of minor suggestions:
- Line 52: Consider using a more specific type than
anyfor the reject function- Line 60: The semicolon after the function declaration is unnecessary
🔧 Optional cleanup
- test('error occurs in click handler can be caught by Vue errorCaptured hook', async () => { + test('error in click handler is caught by Vue errorCaptured hook', async () => { const spyFn = vi.fn(() => false) - let reject: any | null = null + let reject: ((reason: Error) => void) | null = null const wrapper = await mountSuspended({ components: { Button }, setup() { function onClick() { return new Promise<void>((_, rej) => { reject = rej }) - }; + } onErrorCaptured(spyFn)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/components/Button.spec.ts` around lines 50 - 76, The test declares reject as a loosely typed any and has an unnecessary semicolon after the onClick function; update the reject variable to a precise function type such as let reject: ((reason?: unknown) => void) | null (or a PromiseReject type) and remove the trailing semicolon after the onClick() function declaration in the test that mounts Button (references: reject variable, onClick function, mountSuspended, onErrorCaptured, spyFn) so the code is typed correctly and the stray semicolon is cleaned up.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/runtime/components/Button.vue`:
- Line 46: The file imports Vue internals callWithAsyncErrorHandling and
ErrorCodes — remove those imports and any usage of them (references to
callWithAsyncErrorHandling and ErrorCodes) from the Button component, and
instead use public-safe patterns: either wrap your click handler (e.g., the
component's onClick/handleClick method) in a simple try/catch for async errors
or rely on Vue's normal template event handling so errors propagate to app-level
handlers; keep only public imports (computed, ref, inject) and update any
error-handling logic to use try/catch or emit an error event rather than calling
Vue internal APIs.
- Line 136: Replace the undocumented `$` component reference passed into
callWithAsyncErrorHandling with a valid ComponentInternalInstance or null:
update the `@click` invocation that currently calls
callWithAsyncErrorHandling(onClickWrapper, $,
ErrorCodes.COMPONENT_EVENT_HANDLER, [$event]) to pass null (or obtain the actual
internal instance if available) so the second argument matches the required
type; then make error handling consistent with LinkBase.vue by ensuring
LinkBase's direct onClick invocations also wrap handlers with
callWithAsyncErrorHandling (or have Button delegate to LinkBase only after
wrapping), referencing the onClickWrapper function and
callWithAsyncErrorHandling to locate the changes.
---
Nitpick comments:
In `@test/components/Button.spec.ts`:
- Around line 50-76: The test declares reject as a loosely typed any and has an
unnecessary semicolon after the onClick function; update the reject variable to
a precise function type such as let reject: ((reason?: unknown) => void) | null
(or a PromiseReject type) and remove the trailing semicolon after the onClick()
function declaration in the test that mounts Button (references: reject
variable, onClick function, mountSuspended, onErrorCaptured, spyFn) so the code
is typed correctly and the stray semicolon is cleaned up.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 5e6f3876-8ab7-4523-9550-7148f5b43589
📒 Files selected for processing (2)
src/runtime/components/Button.vuetest/components/Button.spec.ts
🔗 Linked issue
Resolves #3237
❓ Type of change
📚 Description
📝 Checklist