diff --git a/package-lock.json b/package-lock.json index bede39959d..c5f5e4a760 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35052,6 +35052,7 @@ "react-refresh": "^0.14.0", "rimraf": "^3.0.2", "stream-browserify": "^3.0.0", + "strip-ansi": "^6.0.0", "util": "^0.12.5", "webpack": "^5.99.9", "webpack-cli": "^6.0.1", diff --git a/packages/browser-repl/package.json b/packages/browser-repl/package.json index b26e20661e..47ea438c08 100644 --- a/packages/browser-repl/package.json +++ b/packages/browser-repl/package.json @@ -113,6 +113,7 @@ "react-refresh": "^0.14.0", "rimraf": "^3.0.2", "stream-browserify": "^3.0.0", + "strip-ansi": "^6.0.0", "util": "^0.12.5", "webpack": "^5.99.9", "webpack-cli": "^6.0.1", diff --git a/packages/browser-repl/src/components/types/error-output.spec.tsx b/packages/browser-repl/src/components/types/error-output.spec.tsx index 0a20a4bdb2..2a46a6646b 100644 --- a/packages/browser-repl/src/components/types/error-output.spec.tsx +++ b/packages/browser-repl/src/components/types/error-output.spec.tsx @@ -54,6 +54,24 @@ describe('ErrorOutput', function () { expect(wrapper.text()).to.contain('Something went wrong.'); }); + + it('strips ANSI codes from syntax errors', function () { + const error = new SyntaxError('Syntax is wrong'); + error.stack = `SyntaxError: Syntax is wrong +\u001b[0m at new Script (vm.js:79:7)\u001b[0m +\u001b[0m at createScript (vm.js:251:10)\u001b[0m +\u001b[0m at Object.runInThisContext (vm.js:303:10)\u001b[0m +\u001b[0m at ...`; + error.message = error.stack; + + const wrapper = mount(); + expect(wrapper.text()).to.deep + .equal(`SyntaxError: SyntaxError: Syntax is wrong + at new Script (vm.js:79:7) + at createScript (vm.js:251:10) + at Object.runInThisContext (vm.js:303:10) + at ...`); + }); }); describe('expanded', function () { @@ -69,5 +87,28 @@ describe('ErrorOutput', function () { ); expect(wrapper.text()).not.to.contain('More details about the error'); }); + + it('strips ANSI codes from syntax errors', function () { + const error = new SyntaxError('Syntax is wrong'); + error.stack = `SyntaxError: Syntax is wrong +\u001b[0m at new Script (vm.js:79:7)\u001b[0m +\u001b[0m at createScript (vm.js:251:10)\u001b[0m +\u001b[0m at Object.runInThisContext (vm.js:303:10)\u001b[0m +\u001b[0m at ...`; + error.message = error.stack; + + const wrapper = mount(); + wrapper.find('svg').simulate('click'); + + expect(wrapper.text()).to.deep + .equal(`SyntaxError: SyntaxError: Syntax is wrong + at new Script (vm.js:79:7) + at createScript (vm.js:251:10) + at Object.runInThisContext (vm.js:303:10) + at ... at new Script (vm.js:79:7) + at createScript (vm.js:251:10) + at Object.runInThisContext (vm.js:303:10) + at ...`); + }); }); }); diff --git a/packages/browser-repl/src/components/types/error-output.tsx b/packages/browser-repl/src/components/types/error-output.tsx index 749c90a94f..449131f9b1 100644 --- a/packages/browser-repl/src/components/types/error-output.tsx +++ b/packages/browser-repl/src/components/types/error-output.tsx @@ -6,6 +6,7 @@ import { css, palette } from '@mongodb-js/compass-components'; import { SimpleTypeOutput } from './simple-type-output'; import { Expandable } from '../utils/expandable'; import type { MongoServerError } from 'mongodb'; +import stripAnsi from 'strip-ansi'; interface ErrorOutputProps { value: any; @@ -31,6 +32,8 @@ export class ErrorOutput extends Component { renderCollapsed(toggle: () => void): JSX.Element { const { name, message, codeName } = this.props.value as MongoServerError; const formattedName = name + (codeName ? `[${codeName}]` : ''); + const strippedMessage = + name === 'SyntaxError' ? stripAnsi(message) : message; return (
@@ -43,14 +46,16 @@ export class ErrorOutput extends Component {
           >
             {formattedName || 'Error'}:
           {' '}
-          {message}
+          {strippedMessage}
         
); } formatStack(): string { - return this.props.value.stack.split('\n').slice(1).join('\n'); + const err = this.props.value; + const stack = err.name === 'SyntaxError' ? stripAnsi(err.stack) : err.stack; + return stack.split('\n').slice(1).join('\n'); } formatErrorBugReportInfo(): JSX.Element | undefined {