Skip to content

Commit 84e69f5

Browse files
authored
Merge pull request #4203 from agamsiingh/copy-error-button-debug-toolbar
Add a Copy button to debug toolbar errors
2 parents 9eb645a + c6e8b00 commit 84e69f5

5 files changed

Lines changed: 160 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
44

55
## [UNRELEASED]
66

7+
### Added
8+
- Add a Copy button to each error in the debug toolbar, which copies the error message and its traceback to the clipboard. Closes [#3624](https://github.com/plotly/dash/issues/3624).
9+
710
### Changed
811
- [#4118](https://github.com/plotly/dash/pull/4118) The JupyterLab extension now ships prebuilt, so JupyterLab 3 and 4 load it without asking for a rebuild. JupyterLab 2 is no longer supported. Fixes [#3828](https://github.com/plotly/dash/issues/3828).
912

‎dash/dash-renderer/src/components/error/FrontEnd/FrontEndError.css‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
}
4545
.dash-fe-error-top__group:first-child {
4646
/*
47-
* 77% is the maximum space allowed based off of the other elements
48-
* in the top part of the error container (timestamp & collapse arrow).
49-
* this was manually determined */
50-
width: 77%;
47+
* 65% is the maximum space allowed based off of the other elements
48+
* in the top part of the error container (copy button, timestamp &
49+
* collapse arrow). this was manually determined */
50+
width: 65%;
5151
}
5252
.dash-fe-error-top__group {
5353
display: inline-flex;
@@ -68,6 +68,21 @@
6868
.dash-fe-error__timestamp {
6969
margin-right: 20px;
7070
}
71+
.dash-fe-error__copy {
72+
white-space: nowrap;
73+
margin-right: 12px;
74+
padding: 0 8px;
75+
font-size: 11px;
76+
line-height: 18px;
77+
color: #506784;
78+
background-color: white;
79+
border: 1px solid #dfe8f3;
80+
border-radius: 3px;
81+
cursor: pointer;
82+
}
83+
.dash-fe-error__copy:hover {
84+
background-color: #f3f6fa;
85+
}
7186
.dash-fe-error__collapse--flipped {
7287
-webkit-transform: rotate(180deg);
7388
-ms-transform: rotate(180deg);

‎dash/dash-renderer/src/components/error/FrontEnd/FrontEndError.react.js‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,44 @@ import CollapseIcon from '../icons/CollapseIcon.svg';
55
import PropTypes from 'prop-types';
66
import '../Percy.css';
77
import {urlBase} from '../../../actions/utils';
8+
import {errorToText, copyToClipboard} from './errorText';
89

910
import werkzeugCss from '../werkzeugcss';
1011

1112
class FrontEndError extends Component {
1213
constructor(props) {
1314
super(props);
1415
this.state = {
15-
collapsed: this.props.isListItem
16+
collapsed: this.props.isListItem,
17+
copyStatus: null
1618
};
19+
this.copyTimeout = null;
20+
this.copyError = this.copyError.bind(this);
21+
}
22+
23+
componentWillUnmount() {
24+
clearTimeout(this.copyTimeout);
25+
}
26+
27+
async copyError(event) {
28+
event.stopPropagation();
29+
let copyStatus = 'copied';
30+
try {
31+
await copyToClipboard(errorToText(this.props.e.error));
32+
} catch (err) {
33+
copyStatus = 'failed';
34+
}
35+
this.setState({copyStatus});
36+
clearTimeout(this.copyTimeout);
37+
this.copyTimeout = setTimeout(
38+
() => this.setState({copyStatus: null}),
39+
1500
40+
);
1741
}
1842

1943
render() {
2044
const {e, inAlertsTray} = this.props;
21-
const {collapsed} = this.state;
45+
const {collapsed, copyStatus} = this.state;
2246

2347
const cardClasses =
2448
'dash-error-card__content' +
@@ -36,6 +60,18 @@ class FrontEndError extends Component {
3660
</span>
3761
</span>
3862
<span className='dash-fe-error-top__group'>
63+
<button
64+
type='button'
65+
className='dash-fe-error__copy test-devtools-error-copy'
66+
title='Copy this error to the clipboard'
67+
onClick={this.copyError}
68+
>
69+
{copyStatus === 'copied'
70+
? 'Copied'
71+
: copyStatus === 'failed'
72+
? 'Copy failed'
73+
: 'Copy'}
74+
</button>
3975
<span className='dash-fe-error__timestamp percy-hide'>
4076
{`${e.timestamp.toLocaleTimeString()}`}
4177
</span>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function htmlToText(html) {
2+
if (!html.trim().startsWith('<')) {
3+
return html;
4+
}
5+
const doc = new DOMParser().parseFromString(html, 'text/html');
6+
const plainTraceback = doc.querySelector('.plain textarea');
7+
if (plainTraceback) {
8+
return plainTraceback.value.trim();
9+
}
10+
return (doc.body.textContent || '').trim();
11+
}
12+
13+
/**
14+
* Builds the plain text of a debug toolbar error so it can be copied:
15+
* the message, the frontend stack trace and the backend traceback.
16+
*/
17+
function errorToText(error) {
18+
const parts = [];
19+
if (typeof error.message === 'string' && error.message) {
20+
parts.push(error.message);
21+
}
22+
if (typeof error.stack === 'string' && error.stack) {
23+
parts.push(error.stack);
24+
}
25+
if (typeof error.html === 'string' && error.html) {
26+
parts.push(htmlToText(error.html));
27+
}
28+
return parts.join('\n\n');
29+
}
30+
31+
async function copyToClipboard(text) {
32+
if (navigator.clipboard && window.isSecureContext) {
33+
try {
34+
await navigator.clipboard.writeText(text);
35+
return;
36+
} catch (e) {
37+
// Permission can be denied, e.g. inside an iframe: use the fallback
38+
}
39+
}
40+
const textarea = document.createElement('textarea');
41+
textarea.value = text;
42+
textarea.style.position = 'fixed';
43+
textarea.style.opacity = '0';
44+
document.body.appendChild(textarea);
45+
textarea.select();
46+
try {
47+
if (!document.execCommand('copy')) {
48+
throw new Error('Copy command was rejected');
49+
}
50+
} finally {
51+
document.body.removeChild(textarea);
52+
}
53+
}
54+
55+
export {errorToText, copyToClipboard};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {expect} from 'chai';
2+
import {describe, it} from 'mocha';
3+
import {errorToText} from '../src/components/error/FrontEnd/errorText';
4+
5+
describe('errorToText', () => {
6+
it('joins the message and the frontend stack trace', () => {
7+
const text = errorToText({
8+
message: 'Something broke',
9+
stack: 'Error: Something broke\n at foo (bar.js:1:1)'
10+
});
11+
expect(text).to.equal(
12+
'Something broke\n\nError: Something broke\n at foo (bar.js:1:1)'
13+
);
14+
});
15+
16+
it('prefers the plain traceback of a werkzeug debugger page', () => {
17+
const html =
18+
'<!doctype html><html><body><div class="debugger">' +
19+
'<h1>ZeroDivisionError</h1><div class="plain">' +
20+
'<textarea cols="50" rows="10" name="code" readonly>' +
21+
'Traceback (most recent call last):\n File "app.py", line 3\n' +
22+
'ZeroDivisionError: division by zero\n</textarea></div></div>' +
23+
'</body></html>';
24+
const text = errorToText({message: 'Callback error', html});
25+
expect(text).to.equal(
26+
'Callback error\n\nTraceback (most recent call last):\n' +
27+
' File "app.py", line 3\nZeroDivisionError: division by zero'
28+
);
29+
});
30+
31+
it('falls back to the text content of an html fragment', () => {
32+
const text = errorToText({html: '<div><b>Oops</b> bad value</div>'});
33+
expect(text).to.equal('Oops bad value');
34+
});
35+
36+
it('returns plain text errors unchanged', () => {
37+
expect(errorToText({html: 'Internal Server Error'})).to.equal(
38+
'Internal Server Error'
39+
);
40+
});
41+
42+
it('returns an empty string when there is nothing to copy', () => {
43+
expect(errorToText({})).to.equal('');
44+
});
45+
});

0 commit comments

Comments
 (0)