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

fix(gatsby-remark-images-contentful): make clear error when loading non-existent image #21756

Merged
merged 3 commits into from
Feb 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 52 additions & 29 deletions packages/gatsby-remark-images-contentful/src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
const Remark = require(`remark`)
const plugin = require(`../`)
const remark = new Remark().data(`settings`, {
commonmark: true,
footnotes: true,
pedantic: true,
})

jest.mock(`../utils/`, () => {
return {
getBase64Img: jest.fn().mockReturnValue(`data:image;`),
Expand All @@ -23,6 +15,19 @@ jest.mock(`../utils/`, () => {
}
})

jest.mock(`axios`)
jest.mock(`sharp`, () => () => {
return {
metadata: jest.fn(() => {
return {
width: 200,
height: 200,
density: 75,
}
}),
}
})

const createNode = content => {
const node = {
id: 1234,
Expand All @@ -47,7 +52,7 @@ const createNode = content => {
return markdownNode
}

const createPluginOptions = (content, imagePaths = `/`) => {
const createPluginOptions = (content, imagePaths = `/`, options = {}) => {
const dirName = `not-a-real-dir`
return {
files: [].concat(imagePaths).map(imagePath => {
Expand All @@ -64,28 +69,28 @@ const createPluginOptions = (content, imagePaths = `/`) => {
}
},
createContentDigest: jest.fn().mockReturnValue(`contentDigest`),
...options,
}
}

jest.mock(`axios`, () => () =>
Promise.resolve({
data: {
pipe: jest.fn(),
destroy: jest.fn(),
},
})
)

jest.mock(`sharp`, () => () => {
return {
metadata: jest.fn(() => {
return {
width: 200,
height: 200,
density: 75,
}
}),
}
const Remark = require(`remark`)
const plugin = require(`../index`)
const remark = new Remark().data(`settings`, {
commonmark: true,
footnotes: true,
pedantic: true,
})
const axios = require(`axios`)

beforeEach(() => {
axios.mockClear()
axios.mockImplementation(() =>
Promise.resolve({
data: {
pipe: jest.fn(),
destroy: jest.fn(),
},
})
)
})

test(`it returns empty array when 0 images`, async () => {
Expand Down Expand Up @@ -153,6 +158,24 @@ test(`it transforms images with a https scheme in markdown`, async () => {
expect(node.value).not.toMatch(`<html>`)
})

test(`it throws specific error if the image is not found`, async () => {
axios.mockImplementationOnce(() => Promise.reject(new Error(`oh no`)))
const reporter = {
panic: jest.fn(),
}
const imagePath = `https://images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/doesnotexist.jpg`
const content = `
![image](${imagePath})
`.trim()

await plugin(createPluginOptions(content, imagePath, { reporter }))
expect(reporter.panic).toHaveBeenCalledTimes(1)
expect(reporter.panic).toHaveBeenCalledWith(
`Image downloading failed for ${imagePath}, please check if the image still exists on contentful`,
expect.any(Error)
)
})

test(`it transforms multiple images in markdown`, async () => {
const imagePaths = [
`//images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/quwowooybuqbl6ntboz3.jpg`,
Expand Down
19 changes: 14 additions & 5 deletions packages/gatsby-remark-images-contentful/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,20 @@ module.exports = async (
}
const metaReader = sharp()

const response = await axios({
method: `GET`,
url: originalImg, // for some reason there is a './' prefix
responseType: `stream`,
})
let response
try {
response = await axios({
method: `GET`,
url: originalImg, // for some reason there is a './' prefix
responseType: `stream`,
})
} catch (err) {
reporter.panic(
`Image downloading failed for ${originalImg}, please check if the image still exists on contentful`,
err
)
return []
}

response.data.pipe(metaReader)

Expand Down