Skip to content

Commit

Permalink
fix: handle errors in formatError and numberToPos (vitejs#4782)
Browse files Browse the repository at this point in the history
  • Loading branch information
milahu committed Aug 30, 2021
1 parent da0abc5 commit 5349c07
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/vite/src/node/server/pluginContainer.ts
Expand Up @@ -285,10 +285,18 @@ export async function createPluginContainer(
: // some rollup plugins, e.g. json, sets position instead of pos
(err as any).position
if (pos != null) {
err.loc = err.loc || {
file: err.id,
...numberToPos(ctx._activeCode, pos)
let errLocation;
try {
errLocation = numberToPos(ctx._activeCode, pos);
}
catch (err2) {
console.log(`formatError: error in numberToPos (${err2}) while handling the error:`);
throw err;
}
err.loc = err.loc || {
file: err.id,
...errLocation
};
err.frame = err.frame || generateCodeFrame(ctx._activeCode, pos)
} else if (err.loc) {
// css preprocessors may report errors in an included file
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Expand Up @@ -273,7 +273,7 @@ export function numberToPos(
): { line: number; column: number } {
if (typeof offset !== 'number') return offset
if (offset > source.length) {
throw new Error('offset is longer than source length!')
throw new Error(`offset is longer than source length! offset ${offset} > length ${source.length}`);
}
const lines = source.split(splitRE)
let counted = 0
Expand Down

0 comments on commit 5349c07

Please sign in to comment.