Skip to content

Commit

Permalink
Better error handling with reporter.panic
Browse files Browse the repository at this point in the history
  • Loading branch information
phacks committed Mar 4, 2019
1 parent fde992e commit cb3902c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ jest.mock(`fs`, () => {
}
})
jest.mock(`recursive-readdir`, () => jest.fn())
jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
panic: jest.fn(),
}
})

const fs = require(`fs`)
const readdir = require(`recursive-readdir`)

const reporter = require(`gatsby-cli/lib/reporter`)

const {
OPTION_DEFAULT_HTML,
OPTION_DEFAULT_REDIRECT_TEMPLATE_PATH,
Expand All @@ -20,6 +27,7 @@ const createPagesParams = {
actions: {
createPage,
},
reporter,
}

describe(`gatsby-remark-code-repls`, () => {
Expand Down
15 changes: 10 additions & 5 deletions packages/gatsby-remark-code-repls/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
} = require(`./constants`)

exports.createPages = async (
{ actions },
{ actions, reporter },
{
directory = OPTION_DEFAULT_LINK_TEXT,
externals = [],
Expand All @@ -27,11 +27,11 @@ exports.createPages = async (
const { createPage } = actions

if (!fs.existsSync(directory)) {
throw Error(`Invalid REPL directory specified: "${directory}"`)
reporter.panic(`Invalid REPL directory specified: "${directory}"`)
}

if (!fs.existsSync(redirectTemplate)) {
throw Error(
reporter.panic(
`Invalid REPL redirectTemplate specified: "${redirectTemplate}"`
)
}
Expand Down Expand Up @@ -75,7 +75,12 @@ exports.createPages = async (
}
})
} catch (error) {
// retrow errors upstream
throw error
reporter.panic(
`
Error in gatsby-remark-code-repls plugin: cannot read directory ${directory}.
More details can be found in the error reporting below.
`,
error
)
}
}

0 comments on commit cb3902c

Please sign in to comment.