Skip to content

Commit

Permalink
fix(gatsby-remark-images-contentful): make clear error when lo… (#21756)
Browse files Browse the repository at this point in the history
Co-authored-by: Ward Peeters <ward@coding-tech.com>
  • Loading branch information
hoangvvo and wardpeet committed Feb 29, 2020
1 parent d357bb1 commit 99e39a0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 34 deletions.
81 changes: 52 additions & 29 deletions packages/gatsby-remark-images-contentful/src/__tests__/index.js
@@ -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
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

0 comments on commit 99e39a0

Please sign in to comment.