Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(site-playground): add missing error handling to fix WSOD (#991) #1154

Merged
merged 1 commit into from Jul 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions site/src/components/editor/editor.module.css
Expand Up @@ -40,3 +40,13 @@
left: 50%;
z-index: 99;
}

.inputError {
color: #fff;
background-color: #bc1515;
padding: 0.3em 1em;
}

.inputError[data-theme='light'] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice touch!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

background-color: #ff5959;
}
14 changes: 14 additions & 0 deletions site/src/components/editor/inputError.js
@@ -0,0 +1,14 @@
import React, { Component } from 'react';
import styles from './editor.module.css';

class InputError extends Component {
render() {
return (
<div className={styles.inputError} data-theme={this.props.theme}>
{this.props.error}
</div>
);
}
}

export default InputError;
40 changes: 34 additions & 6 deletions site/src/pages/playground.js
Expand Up @@ -4,6 +4,7 @@ import prettier from 'prettier/standalone';
import cssParser from 'prettier/parser-postcss';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import className from 'classnames';
import { CssSyntaxError } from 'postcss';
import { RingSpinner as Loader } from '../components/editor/RingSpinner.js';
import MainEditor from '../components/editor/main';
import OutputEditor from '../components/editor/output';
Expand All @@ -14,6 +15,7 @@ import InnerNav from '../components/editor/innerNav.';
import runner from '../components/editor/postcss_runner';
import editorStyles from '../components/editor/editor.module.css';
import CarbonAds from '../components/carbonAds';
import InputError from '../components/editor/inputError.js';
import styles from './styles.module.css';

export default () => {
Expand All @@ -40,6 +42,7 @@ export default () => {
const [output, setOutput] = useState('/* your optimized output here */');
const [input, setInput] = useState(intializedState.input);
const [config, setConfig] = useState(intializedState.config);
const [error, setError] = useState('');

function toggleTheme() {
setTheme(theme === 'light' ? 'vs-dark' : 'light');
Expand All @@ -53,12 +56,35 @@ export default () => {
setInput(value);
}

function handleError(err) {
switch (err.constructor) {
case CssSyntaxError:
setError(`CssSyntaxError: ${err.reason} (${err.line}:${err.column})`);
break;
case SyntaxError:
setError(err.message.split('\n').shift());
break;
default:
setError('Unknown error. See browser console for details.');
console.error(err);
}
}

function resetError() {
setError('');
}

function format() {
const formattedInput = prettier.format(input, {
parser: 'css',
plugins: [cssParser],
});
setInput(formattedInput);
try {
resetError();
const formattedInput = prettier.format(input, {
parser: 'css',
plugins: [cssParser],
});
setInput(formattedInput);
} catch (err) {
handleError(err);
}
}

function saveState() {
Expand Down Expand Up @@ -86,10 +112,11 @@ export default () => {
const resolvedConfig = resolveConfigs(configToSend);
runner(input, resolvedConfig)
.then((res) => {
resetError();
setOutput(res.css);
})
.catch((err) => {
setOutput(err);
handleError(err);
});

setEditorLoading(false);
Expand Down Expand Up @@ -147,6 +174,7 @@ export default () => {
format={format}
save={saveState}
/>
{error && <InputError error={error} theme={theme} />}
<div
className={editorStyles.panelLoaderPlaceholder}
style={{ display: editorLoading ? 'block' : 'none' }}
Expand Down