Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).

## [UNRELEASED]

### Added
- 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).

### Changed
- [#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).

Expand Down
23 changes: 19 additions & 4 deletions dash/dash-renderer/src/components/error/FrontEnd/FrontEndError.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
}
.dash-fe-error-top__group:first-child {
/*
* 77% is the maximum space allowed based off of the other elements
* in the top part of the error container (timestamp & collapse arrow).
* this was manually determined */
width: 77%;
* 65% is the maximum space allowed based off of the other elements
* in the top part of the error container (copy button, timestamp &
* collapse arrow). this was manually determined */
width: 65%;
}
.dash-fe-error-top__group {
display: inline-flex;
Expand All @@ -68,6 +68,21 @@
.dash-fe-error__timestamp {
margin-right: 20px;
}
.dash-fe-error__copy {
white-space: nowrap;
margin-right: 12px;
padding: 0 8px;
font-size: 11px;
line-height: 18px;
color: #506784;
background-color: white;
border: 1px solid #dfe8f3;
border-radius: 3px;
cursor: pointer;
}
.dash-fe-error__copy:hover {
background-color: #f3f6fa;
}
.dash-fe-error__collapse--flipped {
-webkit-transform: rotate(180deg);
-ms-transform: rotate(180deg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,44 @@
import PropTypes from 'prop-types';
import '../Percy.css';
import {urlBase} from '../../../actions/utils';
import {errorToText, copyToClipboard} from './errorText';

import werkzeugCss from '../werkzeugcss';

class FrontEndError extends Component {
constructor(props) {
super(props);
this.state = {
collapsed: this.props.isListItem
collapsed: this.props.isListItem,
copyStatus: null
};
this.copyTimeout = null;
this.copyError = this.copyError.bind(this);
}

componentWillUnmount() {
clearTimeout(this.copyTimeout);
}

async copyError(event) {
event.stopPropagation();
let copyStatus = 'copied';
try {
await copyToClipboard(errorToText(this.props.e.error));
} catch (err) {
copyStatus = 'failed';
}
this.setState({copyStatus});
clearTimeout(this.copyTimeout);
this.copyTimeout = setTimeout(
() => this.setState({copyStatus: null}),
1500
);
}

render() {
const {e, inAlertsTray} = this.props;
const {collapsed} = this.state;
const {collapsed, copyStatus} = this.state;

const cardClasses =
'dash-error-card__content' +
Expand All @@ -36,6 +60,18 @@
</span>
</span>
<span className='dash-fe-error-top__group'>
<button
type='button'
className='dash-fe-error__copy test-devtools-error-copy'
title='Copy this error to the clipboard'
onClick={this.copyError}
>
{copyStatus === 'copied'

Check warning on line 69 in dash/dash-renderer/src/components/error/FrontEnd/FrontEndError.react.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this "===" check; it will always be false. Did you mean to use "=="?

See more on https://sonarcloud.io/project/issues?id=plotly_dash&issues=AaIUiifCIAWqPTtutgiY&open=AaIUiifCIAWqPTtutgiY&pullRequest=4203
? 'Copied'
: copyStatus === 'failed'

Check warning on line 71 in dash/dash-renderer/src/components/error/FrontEnd/FrontEndError.react.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this "===" check; it will always be false. Did you mean to use "=="?

See more on https://sonarcloud.io/project/issues?id=plotly_dash&issues=AaIUiifCIAWqPTtutgia&open=AaIUiifCIAWqPTtutgia&pullRequest=4203
? 'Copy failed'
: 'Copy'}

Check warning on line 73 in dash/dash-renderer/src/components/error/FrontEnd/FrontEndError.react.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=plotly_dash&issues=AaIUiifCIAWqPTtutgiZ&open=AaIUiifCIAWqPTtutgiZ&pullRequest=4203
</button>
<span className='dash-fe-error__timestamp percy-hide'>
{`${e.timestamp.toLocaleTimeString()}`}
</span>
Expand Down
55 changes: 55 additions & 0 deletions dash/dash-renderer/src/components/error/FrontEnd/errorText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function htmlToText(html) {
if (!html.trim().startsWith('<')) {
return html;
}
const doc = new DOMParser().parseFromString(html, 'text/html');
const plainTraceback = doc.querySelector('.plain textarea');
if (plainTraceback) {
return plainTraceback.value.trim();
}
return (doc.body.textContent || '').trim();
}

/**
* Builds the plain text of a debug toolbar error so it can be copied:
* the message, the frontend stack trace and the backend traceback.
*/
function errorToText(error) {
const parts = [];
if (typeof error.message === 'string' && error.message) {
parts.push(error.message);
}
if (typeof error.stack === 'string' && error.stack) {
parts.push(error.stack);
}
if (typeof error.html === 'string' && error.html) {
parts.push(htmlToText(error.html));
}
return parts.join('\n\n');
}

async function copyToClipboard(text) {
if (navigator.clipboard && window.isSecureContext) {
try {
await navigator.clipboard.writeText(text);
return;
} catch (e) {
// Permission can be denied, e.g. inside an iframe: use the fallback
}
}
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
try {
if (!document.execCommand('copy')) {

Check warning on line 47 in dash/dash-renderer/src/components/error/FrontEnd/errorText.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The signature '(commandId: string, showUI?: boolean | undefined, value?: string | undefined): boolean' of 'document.execCommand' is deprecated.

See more on https://sonarcloud.io/project/issues?id=plotly_dash&issues=AaIUiibyIAWqPTtutgiW&open=AaIUiibyIAWqPTtutgiW&pullRequest=4203
throw new Error('Copy command was rejected');
}
} finally {
document.body.removeChild(textarea);

Check warning on line 51 in dash/dash-renderer/src/components/error/FrontEnd/errorText.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `childNode.remove()` over `parentNode.removeChild(childNode)`.

See more on https://sonarcloud.io/project/issues?id=plotly_dash&issues=AaIUiibyIAWqPTtutgiX&open=AaIUiibyIAWqPTtutgiX&pullRequest=4203
}
}

export {errorToText, copyToClipboard};
45 changes: 45 additions & 0 deletions dash/dash-renderer/tests/errorText.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {expect} from 'chai';
import {describe, it} from 'mocha';
import {errorToText} from '../src/components/error/FrontEnd/errorText';

describe('errorToText', () => {
it('joins the message and the frontend stack trace', () => {
const text = errorToText({
message: 'Something broke',
stack: 'Error: Something broke\n at foo (bar.js:1:1)'
});
expect(text).to.equal(
'Something broke\n\nError: Something broke\n at foo (bar.js:1:1)'
);
});

it('prefers the plain traceback of a werkzeug debugger page', () => {
const html =
'<!doctype html><html><body><div class="debugger">' +
'<h1>ZeroDivisionError</h1><div class="plain">' +
'<textarea cols="50" rows="10" name="code" readonly>' +
'Traceback (most recent call last):\n File "app.py", line 3\n' +
'ZeroDivisionError: division by zero\n</textarea></div></div>' +
'</body></html>';
const text = errorToText({message: 'Callback error', html});
expect(text).to.equal(
'Callback error\n\nTraceback (most recent call last):\n' +
' File "app.py", line 3\nZeroDivisionError: division by zero'
);
});

it('falls back to the text content of an html fragment', () => {
const text = errorToText({html: '<div><b>Oops</b> bad value</div>'});
expect(text).to.equal('Oops bad value');
});

it('returns plain text errors unchanged', () => {
expect(errorToText({html: 'Internal Server Error'})).to.equal(
'Internal Server Error'
);
});

it('returns an empty string when there is nothing to copy', () => {
expect(errorToText({})).to.equal('');
});
});
Loading