Skip to content

Commit cb3902c

Browse files
committed
Better error handling with reporter.panic
1 parent fde992e commit cb3902c

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

packages/gatsby-remark-code-repls/src/__tests__/gatsby-node.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ jest.mock(`fs`, () => {
55
}
66
})
77
jest.mock(`recursive-readdir`, () => jest.fn())
8+
jest.mock(`gatsby-cli/lib/reporter`, () => {
9+
return {
10+
panic: jest.fn(),
11+
}
12+
})
813

914
const fs = require(`fs`)
1015
const readdir = require(`recursive-readdir`)
1116

17+
const reporter = require(`gatsby-cli/lib/reporter`)
18+
1219
const {
1320
OPTION_DEFAULT_HTML,
1421
OPTION_DEFAULT_REDIRECT_TEMPLATE_PATH,
@@ -20,6 +27,7 @@ const createPagesParams = {
2027
actions: {
2128
createPage,
2229
},
30+
reporter,
2331
}
2432

2533
describe(`gatsby-remark-code-repls`, () => {

packages/gatsby-remark-code-repls/src/gatsby-node.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const {
1212
} = require(`./constants`)
1313

1414
exports.createPages = async (
15-
{ actions },
15+
{ actions, reporter },
1616
{
1717
directory = OPTION_DEFAULT_LINK_TEXT,
1818
externals = [],
@@ -27,11 +27,11 @@ exports.createPages = async (
2727
const { createPage } = actions
2828

2929
if (!fs.existsSync(directory)) {
30-
throw Error(`Invalid REPL directory specified: "${directory}"`)
30+
reporter.panic(`Invalid REPL directory specified: "${directory}"`)
3131
}
3232

3333
if (!fs.existsSync(redirectTemplate)) {
34-
throw Error(
34+
reporter.panic(
3535
`Invalid REPL redirectTemplate specified: "${redirectTemplate}"`
3636
)
3737
}
@@ -75,7 +75,12 @@ exports.createPages = async (
7575
}
7676
})
7777
} catch (error) {
78-
// retrow errors upstream
79-
throw error
78+
reporter.panic(
79+
`
80+
Error in gatsby-remark-code-repls plugin: cannot read directory ${directory}.
81+
More details can be found in the error reporting below.
82+
`,
83+
error
84+
)
8085
}
8186
}

0 commit comments

Comments
 (0)