Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Minify error messages in production (#21214)
- Loading branch information
Showing
40 changed files
with
640 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs'; | ||
import { prepareMarkdown } from 'docs/src/modules/utils/parseMarkdown'; | ||
|
||
const pageFilename = 'production-error'; | ||
const requireDemo = require.context('docs/src/pages/production-error', false, /\.js$/); | ||
const requireRaw = require.context( | ||
'!raw-loader!../src/pages/production-error', | ||
false, | ||
/\.(js|md)$/, | ||
); | ||
|
||
export default function Page({ demos, docs }) { | ||
return <MarkdownDocs demos={demos} disableAd docs={docs} requireDemo={requireDemo} />; | ||
} | ||
|
||
Page.getInitialProps = () => { | ||
const { demos, docs } = prepareMarkdown({ pageFilename, requireRaw }); | ||
return { demos, docs }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"1": "Material-UI: Expected valid input target. Did you use a custom `inputComponent` and forget to forward refs? See https://material-ui.com/r/input-component-ref-interface for more info.", | ||
"2": "Material-UI: The `value` prop must be an array when using the `Select` component with `multiple`.", | ||
"3": "Material-UI: Unsupported `%s` color.\nWe support the following formats: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla().", | ||
"4": "Material-UI: The color provided to augmentColor(color) is invalid.\nThe color object needs to have a `main` property or a `%s` property.", | ||
"5": "Material-UI: The color provided to augmentColor(color) is invalid.\n`color.main` should be a string, but `%s` was provided instead.\n\nDid you intend to use one of the following approaches?\n\nimport { green } from \"@material-ui/core/colors\";\n\nconst theme1 = createMuiTheme({ palette: {\n primary: green,\n} });\n\nconst theme2 = createMuiTheme({ palette: {\n primary: { main: green[500] },\n} });", | ||
"6": "Material-UI: Unsupported non-unitless line height with grid alignment.\nUse unitless line heights instead.", | ||
"7": "Material-UI: capitalize(string) expects a string argument." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import * as React from 'react'; | ||
import { useRouter } from 'next/router'; | ||
import Link from '@material-ui/core/Link'; | ||
import Typography from '@material-ui/core/Typography'; | ||
import { styled } from '@material-ui/core/styles'; | ||
import MarkdownElement from 'docs/src/modules/components/MarkdownElement'; | ||
import { render as renderMarkdown } from 'docs/src/modules/utils/parseMarkdown'; | ||
|
||
const ErrorMessageSection = styled('div')({ | ||
// reset display: block from Demo | ||
display: 'block', | ||
}); | ||
|
||
// use elevation={2} | ||
const ErrorMessageMarkdown = styled(MarkdownElement)(({ theme }) => { | ||
return { | ||
boxShadow: theme.shadows['2'], | ||
color: theme.palette.error.main, | ||
padding: theme.spacing(1, 2), | ||
}; | ||
}); | ||
|
||
export default function ErrorDecoder() { | ||
const { | ||
query: { code, ...query }, | ||
} = useRouter(); | ||
const queryArgs = query['args[]']; | ||
const args = React.useMemo(() => (Array.isArray(queryArgs) ? queryArgs : [queryArgs]), [ | ||
queryArgs, | ||
]); | ||
|
||
const [data, dispatch] = React.useReducer( | ||
(previousState, action) => { | ||
switch (action.type) { | ||
case 'rejected': | ||
return { errorCodes: null, state: 'rejected' }; | ||
case 'resolved': | ||
return { errorCodes: action.payload, state: 'resolved' }; | ||
default: | ||
throw new Error(`We made a mistake passing an unknown action.`); | ||
} | ||
}, | ||
{ errorCodes: null, state: 'loading' }, | ||
); | ||
|
||
React.useEffect(() => { | ||
let cancelled = false; | ||
|
||
fetch('/static/error-codes.json') | ||
.then((response) => { | ||
return response.json(); | ||
}) | ||
.then((json) => { | ||
if (cancelled === false) { | ||
dispatch({ type: 'resolved', payload: json }); | ||
} | ||
}) | ||
.catch(() => { | ||
dispatch({ type: 'rejected' }); | ||
}); | ||
|
||
return () => { | ||
cancelled = true; | ||
}; | ||
}, []); | ||
|
||
const errorMessage = React.useMemo(() => { | ||
const rawMessage = data.errorCodes?.[code]; | ||
if (rawMessage === undefined) { | ||
return undefined; | ||
} | ||
|
||
let replacementIndex = -1; | ||
const readableMessage = rawMessage.replace(/%s/g, () => { | ||
replacementIndex += 1; | ||
const dangerousArgument = args[replacementIndex]; | ||
if (dangerousArgument === undefined) { | ||
return '[missing argument]'; | ||
} | ||
// String will be injected into innerHTML. | ||
// We need to escape first | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML#Security_considerations | ||
const div = document.createElement('div'); | ||
div.innerText = dangerousArgument; | ||
return div.innerHTML; | ||
}); | ||
|
||
return renderMarkdown(readableMessage); | ||
}, [args, code, data.errorCodes]); | ||
|
||
if (data.state === 'loading') { | ||
return <Typography>Loading error codes</Typography>; | ||
} | ||
|
||
if (data.state === 'rejected') { | ||
return ( | ||
<Typography color="error"> | ||
Seems like we're having some issues loading the original message. Try reloading the | ||
page. If the error persists please report this isse on our{' '} | ||
<Link | ||
href="https://github.com/mui-org/material-ui/issues/new?template=1.bug.md" | ||
target="_blank" | ||
> | ||
issue tracker | ||
</Link> | ||
. | ||
</Typography> | ||
); | ||
} | ||
|
||
if (errorMessage === undefined) { | ||
return ( | ||
<Typography> | ||
When you encounter an error, you'll receive a link to this page for that specific error | ||
and we'll show you the full error text. | ||
</Typography> | ||
); | ||
} | ||
|
||
return ( | ||
<ErrorMessageSection> | ||
<p>The original text of the error you encountered:</p> | ||
<ErrorMessageMarkdown renderedMarkdown={errorMessage} /> | ||
</ErrorMessageSection> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Production Error | ||
|
||
<p class="description">In the production build of Material-UI error messages are minified to reduce the size of your application.</p> | ||
|
||
We recommend using the development build when debugging this error. | ||
It will include additional warnings about potential problems. | ||
If you encounter an exception while using the production build, this page will reassemble the orrigianl text of the error. | ||
|
||
{{"demo": "pages/production-error/ErrorDecoder.js", "hideToolbar": true, "bg": "inline"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.