Skip to content

Commit

Permalink
Update error for failing to parse custom-route source (#10197)
Browse files Browse the repository at this point in the history
* De-dupe invalid-custom-routes tests

* Update error for failing to parse custom-route source
  • Loading branch information
ijjk authored and timneutkens committed Jan 22, 2020
1 parent fe72fd2 commit b6edf81
Show file tree
Hide file tree
Showing 2 changed files with 289 additions and 249 deletions.
26 changes: 20 additions & 6 deletions packages/next/lib/check-custom-routes.ts
Expand Up @@ -126,18 +126,32 @@ export default function checkCustomRoutes(
invalidParts.push(...result.invalidParts)
}

if (typeof route.source === 'string') {
if (typeof route.source === 'string' && route.source.startsWith('/')) {
// only show parse error if we didn't already show error
// for not being a string
try {
// Make sure we can parse the source properly
regexpMatch(route.source)
} catch (err) {
// If there is an error show our err.sh but still show original error
console.error(
`\nError parsing ${route.source} https://err.sh/zeit/next.js/invalid-route-source`,
err
)
// If there is an error show our err.sh but still show original error or a formatted one if we can
const errMatches = err.message.match(/at (\d{0,})/)

if (errMatches) {
const position = parseInt(errMatches[1], 10)
console.error(
`\nError parsing \`${route.source}\` ` +
`https://err.sh/zeit/next.js/invalid-route-source\n` +
`Reason: ${err.message}\n\n` +
` ${route.source}\n` +
` ${new Array(position).fill(' ').join('')}^\n`
)
} else {
console.error(
`\nError parsing ${route.source} https://err.sh/zeit/next.js/invalid-route-source`,
err
)
}
invalidParts.push('`source` parse failed')
}
}

Expand Down

0 comments on commit b6edf81

Please sign in to comment.