Skip to content

Commit eb7bcd6

Browse files
authored
fix(browser-repl): strip ansi codes from syntax errors COMPASS-9921 (#2550)
strip ansi codes from syntax errors in browser-repl
1 parent de38736 commit eb7bcd6

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/browser-repl/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
"react-refresh": "^0.14.0",
114114
"rimraf": "^3.0.2",
115115
"stream-browserify": "^3.0.0",
116+
"strip-ansi": "^6.0.0",
116117
"util": "^0.12.5",
117118
"webpack": "^5.99.9",
118119
"webpack-cli": "^6.0.1",

packages/browser-repl/src/components/types/error-output.spec.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ describe('ErrorOutput', function () {
5454

5555
expect(wrapper.text()).to.contain('Something went wrong.');
5656
});
57+
58+
it('strips ANSI codes from syntax errors', function () {
59+
const error = new SyntaxError('Syntax is wrong');
60+
error.stack = `SyntaxError: Syntax is wrong
61+
\u001b[0m at new Script (vm.js:79:7)\u001b[0m
62+
\u001b[0m at createScript (vm.js:251:10)\u001b[0m
63+
\u001b[0m at Object.runInThisContext (vm.js:303:10)\u001b[0m
64+
\u001b[0m at ...`;
65+
error.message = error.stack;
66+
67+
const wrapper = mount(<ErrorOutput value={error} />);
68+
expect(wrapper.text()).to.deep
69+
.equal(`SyntaxError: SyntaxError: Syntax is wrong
70+
at new Script (vm.js:79:7)
71+
at createScript (vm.js:251:10)
72+
at Object.runInThisContext (vm.js:303:10)
73+
at ...`);
74+
});
5775
});
5876

5977
describe('expanded', function () {
@@ -69,5 +87,28 @@ describe('ErrorOutput', function () {
6987
);
7088
expect(wrapper.text()).not.to.contain('More details about the error');
7189
});
90+
91+
it('strips ANSI codes from syntax errors', function () {
92+
const error = new SyntaxError('Syntax is wrong');
93+
error.stack = `SyntaxError: Syntax is wrong
94+
\u001b[0m at new Script (vm.js:79:7)\u001b[0m
95+
\u001b[0m at createScript (vm.js:251:10)\u001b[0m
96+
\u001b[0m at Object.runInThisContext (vm.js:303:10)\u001b[0m
97+
\u001b[0m at ...`;
98+
error.message = error.stack;
99+
100+
const wrapper = mount(<ErrorOutput value={error} />);
101+
wrapper.find('svg').simulate('click');
102+
103+
expect(wrapper.text()).to.deep
104+
.equal(`SyntaxError: SyntaxError: Syntax is wrong
105+
at new Script (vm.js:79:7)
106+
at createScript (vm.js:251:10)
107+
at Object.runInThisContext (vm.js:303:10)
108+
at ... at new Script (vm.js:79:7)
109+
at createScript (vm.js:251:10)
110+
at Object.runInThisContext (vm.js:303:10)
111+
at ...`);
112+
});
72113
});
73114
});

packages/browser-repl/src/components/types/error-output.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { css, palette } from '@mongodb-js/compass-components';
66
import { SimpleTypeOutput } from './simple-type-output';
77
import { Expandable } from '../utils/expandable';
88
import type { MongoServerError } from 'mongodb';
9+
import stripAnsi from 'strip-ansi';
910

1011
interface ErrorOutputProps {
1112
value: any;
@@ -31,6 +32,8 @@ export class ErrorOutput extends Component<ErrorOutputProps> {
3132
renderCollapsed(toggle: () => void): JSX.Element {
3233
const { name, message, codeName } = this.props.value as MongoServerError;
3334
const formattedName = name + (codeName ? `[${codeName}]` : '');
35+
const strippedMessage =
36+
name === 'SyntaxError' ? stripAnsi(message) : message;
3437
return (
3538
<div>
3639
<pre>
@@ -43,14 +46,16 @@ export class ErrorOutput extends Component<ErrorOutputProps> {
4346
>
4447
{formattedName || 'Error'}:
4548
</a>{' '}
46-
<span className={messageCss}>{message}</span>
49+
<span className={messageCss}>{strippedMessage}</span>
4750
</pre>
4851
</div>
4952
);
5053
}
5154

5255
formatStack(): string {
53-
return this.props.value.stack.split('\n').slice(1).join('\n');
56+
const err = this.props.value;
57+
const stack = err.name === 'SyntaxError' ? stripAnsi(err.stack) : err.stack;
58+
return stack.split('\n').slice(1).join('\n');
5459
}
5560

5661
formatErrorBugReportInfo(): JSX.Element | undefined {

0 commit comments

Comments
 (0)