Skip to content

Commit 77e9aab

Browse files
authored
fix(gatsby-remark-image-contentful): show useful error message for files that can not be rendered as image (#32530)
* fix: show useful error message for files that can not be rendered as image fixes #32511 * test(unit): improve sharp mock
1 parent f5dab4f commit 77e9aab

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

packages/gatsby-remark-images-contentful/src/__tests__/index.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,33 @@ jest.mock(`../utils/`, () => {
1616
})
1717

1818
jest.mock(`axios`)
19-
jest.mock(`sharp`, () => () => {
20-
return {
21-
metadata: jest.fn(() => {
22-
return {
23-
width: 200,
24-
height: 200,
25-
density: 75,
26-
}
27-
}),
19+
20+
jest.mock(`sharp`, () => {
21+
const metadataMock = jest.fn(() => {
22+
return {
23+
width: 200,
24+
height: 200,
25+
density: 75,
26+
}
27+
})
28+
29+
const sharp = () => {
30+
const pipeline = {
31+
metadata: metadataMock,
32+
}
33+
return pipeline
2834
}
35+
36+
sharp.metadataMock = metadataMock
37+
38+
return sharp
2939
})
3040

41+
const sharp = require(`sharp`)
42+
const mockSharpFailure = () => {
43+
sharp.metadataMock.mockRejectedValueOnce(new Error(`invalid image`))
44+
}
45+
3146
const createNode = content => {
3247
const node = {
3348
id: 1234,
@@ -131,7 +146,6 @@ test(`it transforms images in markdown`, async () => {
131146
const content = `
132147
![image](${imagePath})
133148
`.trim()
134-
135149
const nodes = await plugin(createPluginOptions(content, imagePath))
136150

137151
expect(nodes.length).toBe(1)
@@ -237,3 +251,25 @@ test(`it transforms images in markdown with webp srcSets if option is enabled`,
237251
expect(node.value).toMatchSnapshot()
238252
expect(node.value).not.toMatch(`<html>`)
239253
})
254+
255+
test(`it shows an useful error message when the file is not a valid image`, async () => {
256+
mockSharpFailure()
257+
258+
const imagePath = `//images.ctfassets.net/k8iqpp6u0ior/752jwCIe9dwtfi9mLbp9m2/bc588ee25cf8299bc33a56ca32f8677b/Gatsby-Logos.zip`
259+
260+
const content = `
261+
![image](${imagePath})
262+
`.trim()
263+
264+
const reporter = {
265+
panic: jest.fn(),
266+
}
267+
268+
await plugin(createPluginOptions(content, imagePath, { reporter }))
269+
270+
expect(reporter.panic).toHaveBeenCalledTimes(1)
271+
expect(reporter.panic).toHaveBeenCalledWith(
272+
`The image "${imagePath}" (with alt text: "image") doesn't appear to be a supported image format.`,
273+
expect.any(Error)
274+
)
275+
})

packages/gatsby-remark-images-contentful/src/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ module.exports = async (
6464
}
6565
const metaReader = sharp()
6666

67+
// @todo to increase reliablility, this should use the asset downloading function from gatsby-source-contentful
6768
let response
6869
try {
6970
response = await axios({
@@ -81,7 +82,15 @@ module.exports = async (
8182

8283
response.data.pipe(metaReader)
8384

84-
const metadata = await metaReader.metadata()
85+
let metadata
86+
try {
87+
metadata = await metaReader.metadata()
88+
} catch (error) {
89+
reporter.panic(
90+
`The image "${node.url}" (with alt text: "${node.alt}") doesn't appear to be a supported image format.`,
91+
error
92+
)
93+
}
8594

8695
response.data.destroy()
8796

0 commit comments

Comments
 (0)