Skip to content

Commit

Permalink
feat: provide file path info in error object (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn authored Jan 11, 2020
1 parent 778746e commit d439345
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export interface PageMeta {
routeMeta?: any
}

interface FileError extends Error {
file?: string
}

const routeMetaName = 'route-meta'

export function resolveRoutePaths(
Expand Down Expand Up @@ -48,7 +52,9 @@ function pathMapToMeta(
}

const content = readFile(path.join('/'))
const parsed = parseComponent(content)
const parsed = parseComponent(content, {
pad: 'space'
})
const routeMetaBlock = parsed.customBlocks.find(
b => b.type === routeMetaName
)
Expand All @@ -57,10 +63,17 @@ function pathMapToMeta(
try {
meta.routeMeta = JSON.parse(routeMetaBlock.content)
} catch (err) {
throw new Error(
`Invalid json format of <route-meta> content in ${path.join('/')}\n` +
const joinedPath = path.join('/')
const wrapped: FileError = new Error(
`Invalid json format of <route-meta> content in ${joinedPath}\n` +
err.message
)

// Store file path to provide useful information to downstream tools
// like friendly-errors-webpack-plugin
wrapped.file = joinedPath

throw wrapped
}
}

Expand Down

0 comments on commit d439345

Please sign in to comment.