Skip to content

fix: add error handling to async clipboard handler in header.tsx#746

Open
96528025 wants to merge 2 commits intoOpenCut-app:mainfrom
96528025:fix/clipboard-error-handling
Open

fix: add error handling to async clipboard handler in header.tsx#746
96528025 wants to merge 2 commits intoOpenCut-app:mainfrom
96528025:fix/clipboard-error-handling

Conversation

@96528025
Copy link

@96528025 96528025 commented Mar 18, 2026

Summary

Fixes #722

The async onClick handler for copying the SVG logo had three unhandled promise rejections with no user feedback on failure.

  • Wrap fetch(), res.text(), and navigator.clipboard.writeText() in a try-catch block
  • Show toast.success on successful copy
  • Show toast.error if any step fails

Test plan

  • Right-click the OpenCut logo in the header
  • Click "Copy SVG" — confirm success toast appears and SVG is in clipboard
  • Test in a browser that blocks clipboard access — confirm error toast appears instead of silent failure

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added user-facing toast notifications for SVG copy actions (success and error).
  • Bug Fixes

    • Improved SVG copy behavior with robust error handling and clearer feedback when the copy fails.
Wrap the async onClick handler for copying the SVG logo in a try-catch block to handle potential failures from fetch(), res.text(), and navigator.clipboard.writeText(). Show a success toast on copy and an error toast if any step fails. Fixes OpenCut-app#722 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Mar 18, 2026

@96528025 is attempting to deploy a commit to the OpenCut OSS Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 03c81de8-1c0a-46d5-8df3-ad4c2c51d0ee

📥 Commits

Reviewing files that changed from the base of the PR and between 29504c8 and c1613d8.

📒 Files selected for processing (1)
  • apps/web/src/components/header.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/web/src/components/header.tsx

📝 Walkthrough

Walkthrough

Adds try/catch around the SVG logo copy-to-clipboard flow in the header component and shows success/error toasts using the sonner library instead of silently failing. Changes affect only the click handler logic for copying the SVG.

Changes

Cohort / File(s) Summary
Clipboard Error Handling
apps/web/src/components/header.tsx
Wraps the SVG fetch/text/clipboard steps in a try/catch and uses sonner to display success and error toast notifications; replaces silent failures with explicit feedback.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 I nudged the logo, once lost in air,
Now a toast sings out when it’s copied with care,
Errors no longer hide,
Success hops with pride,
Clipboard and rabbit — a cheerful pair 🥕✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding error handling to an async clipboard handler in header.tsx.
Description check ✅ Passed The description addresses the bug, links to issue #722, outlines specific changes, and includes a test plan, though it lacks the required approval confirmation from maintainers.
Linked Issues check ✅ Passed The PR successfully implements all requirements from issue #722: wrapping async operations in try-catch, adding success/error toast feedback, and checking HTTP response.ok.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the clipboard handler as per issue #722; renaming 'res' to 'response' is a minor improvement directly related to the error handling fix.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Inline comments: In `@apps/web/src/components/header.tsx`: - Around line 70-77: Change the fetch block to properly check HTTP failures and use a non-abbreviated variable name: when fetching DEFAULT_LOGO_URL use "response" (not "res"), verify response.ok and throw or call toast.error if it's false before calling response.text(), then await navigator.clipboard.writeText(svg) and call toast.success on success; ensure the catch still triggers toast.error for network/other errors (update the block around navigator.clipboard.writeText and toast.success/toast.error accordingly). 

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c2b7ccfe-dffb-46f5-bd66-3fe70ab64080

📥 Commits

Reviewing files that changed from the base of the PR and between 26d523e and 29504c8.

📒 Files selected for processing (1)
  • apps/web/src/components/header.tsx
Comment on lines +70 to +77
try {
const res = await fetch(DEFAULT_LOGO_URL);
const svg = await res.text();
await navigator.clipboard.writeText(svg);
toast.success("SVG copied to clipboard");
} catch {
toast.error("Failed to copy SVG to clipboard");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash # Verify whether the copy handler checks HTTP status before success toast. # Expected: explicit `response.ok` guard in the same handler block. rg -n -C4 'fetch\(DEFAULT_LOGO_URL\)|response\.ok|toast\.success\("SVG copied to clipboard"\)' apps/web/src/components/header.tsx

Repository: OpenCut-app/OpenCut

Length of output: 491


Guard HTTP failure responses and fix variable naming.

The fetch() at line 71 can resolve with non-2xx status codes without checking response.ok, allowing error responses to proceed to the success toast at line 74, which misleadingly indicates the SVG was successfully copied when it may have been an error page. Additionally, the variable res violates the no-abbreviation naming guideline; use response instead.

🔧 Suggested patch
	onClick={async () => {	try { -const res = await fetch(DEFAULT_LOGO_URL); -const svg = await res.text(); -await navigator.clipboard.writeText(svg); +const response = await fetch(DEFAULT_LOGO_URL); +if (!response.ok) { +throw new Error( +`Failed to fetch SVG: ${response.status}`, +); +} +const svgMarkup = await response.text(); +await navigator.clipboard.writeText(svgMarkup);	toast.success("SVG copied to clipboard");	} catch {	toast.error("Failed to copy SVG to clipboard");	}	}}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/web/src/components/header.tsx` around lines 70 - 77, Change the fetch block to properly check HTTP failures and use a non-abbreviated variable name: when fetching DEFAULT_LOGO_URL use "response" (not "res"), verify response.ok and throw or call toast.error if it's false before calling response.text(), then await navigator.clipboard.writeText(svg) and call toast.success on success; ensure the catch still triggers toast.error for network/other errors (update the block around navigator.clipboard.writeText and toast.success/toast.error accordingly). 
- Rename `res` to `response` to follow no-abbreviation naming guideline - Add `response.ok` check to throw on non-2xx status codes, preventing error page content from being silently copied to the clipboard Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant