Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve @next/font error handling (#43298)
Use `WellKnownErrorsPlugin` when formatting `@next/font` errors. The error handling is different for expected errors (like validation errors) and unknown ones. If the error is expected it just displays the formatted error message. If it's an unknown error it will also display the stacktrace, and in case the versions of `@next/font` and `next` are different it will encourage you to try to update them both. Removes the usage of `error-loader` when importing from `pages/_document.js`, handle that case with `WellKnownErrorsPlugin` as well. The current warning when using different versions is removed. If the versions are incompatible you'll get an error in the overlay instead. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
- Loading branch information
Hannes Bornö
committed
Nov 24, 2022
1 parent
ec7609e
commit 5788f60
Showing
20 changed files
with
203 additions
and
96 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
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
45 changes: 45 additions & 0 deletions
45
packages/next/build/webpack/plugins/wellknown-errors-plugin/parseNextFontError.ts
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,45 @@ | ||
import { SimpleWebpackError } from './simpleWebpackError' | ||
|
||
export function getNextFontError( | ||
err: Error, | ||
module: any | ||
): SimpleWebpackError | false { | ||
try { | ||
const resourceResolveData = module.resourceResolveData | ||
if (resourceResolveData.descriptionFileData.name !== '@next/font') { | ||
return false | ||
} | ||
|
||
// Parse the query and get the path of the file where the font function was called. | ||
// provided by next-swc next_font_loaders | ||
const file = JSON.parse(resourceResolveData.query.slice(1)).path | ||
|
||
if (err.name === 'NextFontError') { | ||
// Known error thrown by @next/font, display the error message | ||
return new SimpleWebpackError( | ||
file, | ||
`\`@next/font\` error:\n${err.message}` | ||
) | ||
} else { | ||
// Unknown error thrown by @next/font | ||
// It might be becuase of incompatible versions of @next/font and next are being used, or it might be a bug | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
const nextFontVersion = require('@next/font/package.json').version | ||
const nextVersion = require('next/package.json').version | ||
|
||
let message = `An error occured in \`@next/font\`.` | ||
|
||
// Using different versions of @next/font and next, add message that it's possibly fixed by updating both | ||
if (nextFontVersion !== nextVersion) { | ||
message += `\n\nYou might be using incompatible version of \`@next/font\` (${nextFontVersion}) and \`next\` (${nextVersion}). Try updating both \`@next/font\` and \`next\`, if the error still persists it may be a bug.` | ||
} | ||
|
||
message += `\n\n${err.stack}` | ||
|
||
return new SimpleWebpackError(file, message) | ||
} | ||
} catch { | ||
return false | ||
} | ||
} |
Oops, something went wrong.