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

Update error for failing to parse custom-route source #10197

Merged
merged 4 commits into from Jan 22, 2020
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
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