laravel/framework · warning · Error
Failed to copy text to clipboard
Error message
Failed to copy text to clipboard
What it means
Thrown by the exception renderer's client-side clipboard helper when document.execCommand('copy') returns a falsy result. The library uses a legacy textarea technique as a fallback for copying stack traces / code snippets. It indicates the browser refused or could not perform the copy operation.
Solutions
- Serve the error page over HTTPS or access it via localhost/127.0.0.1 so the Clipboard API is permitted.
- Ensure the document has focus when the copy button is clicked (do not background the tab).
- If embedded in an iframe, add allow="clipboard-write" to the iframe element.
- Upgrade the browser to one that supports navigator.clipboard.writeText and confirm the renderer's window.copy path.
- As a manual fallback, select the text in the rendered stack trace and use Ctrl/Cmd+C.
Example fix
// before — relies on execCommand, fails on insecure origins
const result = document.execCommand('copy');
if (!result) throw new Error('Failed to copy text to clipboard');
// after — prefer async Clipboard API, fall back to execCommand
async function copyText(text) {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
return;
}
const ta = document.createElement('textarea');
ta.value = text;
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
const ok = document.execCommand('copy');
document.body.removeChild(ta);
if (!ok) throw new Error('Failed to copy text to clipboard');
} Defensive patterns
Strategy: try-catch
Validate before calling
// guard before invoking the copy helper
function canCopyViaExec() {
return typeof document !== 'undefined' && typeof document.execCommand === 'function' && document.hasFocus();
}
function canCopyViaClipboardApi() {
return typeof navigator !== 'undefined' && !!navigator.clipboard && window.isSecureContext;
} Type guard
function isClipboardAvailable(): boolean {
return (typeof navigator !== 'undefined' && !!navigator.clipboard && window.isSecureContext)
|| (typeof document !== 'undefined' && typeof document.execCommand === 'function');
} Try / catch
try {
await copyText(traceText);
} catch (e) {
// graceful degradation: surface the text for manual selection/copy
prompt('Copy with Ctrl/Cmd+C:', traceText);
} Prevention
- Serve the error page over HTTPS or use localhost.
- Prefer navigator.clipboard.writeText in secure contexts.
- Add allow="clipboard-write" if the page is iframe-embedded.
When it happens
Trigger: Calling the copy-to-clipboard action from the ignition-style error page when execCommand('copy') fails — e.g. the page is served over an insecure (non-HTTPS, non-localhost) origin, the document lost focus, the browser disabled execCommand, or a Permissions Policy blocks clipboard-write.
Common situations: Opening the rendered exception page over plain HTTP on a remote host; the tab being backgrounded at click time; running a browser that has removed execCommand; iframe embedding without allow="clipboard-write".
Related errors
AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11).
Data as JSON: /api/errors/6208705c99606be5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Illuminate/Foundation/resources/exceptions/renderer/scripts.js:36
});
window.copyToClipboard = async function (text) {
if (navigator.clipboard) {
await navigator.clipboard.writeText(text);
} else {
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
textarea.style.pointerEvents = 'none';
document.body.appendChild(textarea);
textarea.select();
const result = document.execCommand('copy');
document.body.removeChild(textarea);
if (!result) {
throw new Error('Failed to copy text to clipboard');
}
}
};
const highlighter = createHighlighterCoreSync({
themes: [lightPlus, darkPlus],
langs: [php, sql, json],
engine: createJavaScriptRegexEngine(),
});
window.highlight = function (
code,
language,
truncate = false,
editor = false,
startingLine = 1,
highlightedLine = null
) {View on GitHub (pinned to e0f6eb3518)