From c7347e70fed5431b00f1e4ae1475a285039f7de5 Mon Sep 17 00:00:00 2001 From: Hoang Vo <40987398+hoangvvo@users.noreply.github.com> Date: Wed, 26 Feb 2020 01:48:19 -0500 Subject: [PATCH 1/2] Rethrow the error when failing to fetch Contentful image --- .../src/__tests__/index.js | 11 +++++++++++ .../src/index.js | 17 ++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/gatsby-remark-images-contentful/src/__tests__/index.js b/packages/gatsby-remark-images-contentful/src/__tests__/index.js index f093ecc237bc2..cc5fd1c65830b 100644 --- a/packages/gatsby-remark-images-contentful/src/__tests__/index.js +++ b/packages/gatsby-remark-images-contentful/src/__tests__/index.js @@ -153,6 +153,17 @@ test(`it transforms images with a https scheme in markdown`, async () => { expect(node.value).not.toMatch(``) }) +test(`it throws specific error if the image is not found`, async () => { + const imagePath = `https://images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/doesnotexist.jpg` + const content = ` +![image](${imagePath}) + `.trim() + + await expect(plugin(createPluginOptions(content, imagePath))).rejects.toThrow( + `Image downloading failed for ${imagePath}, please check if the image still exists on contentful` + ) +}) + test(`it transforms multiple images in markdown`, async () => { const imagePaths = [ `//images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/quwowooybuqbl6ntboz3.jpg`, diff --git a/packages/gatsby-remark-images-contentful/src/index.js b/packages/gatsby-remark-images-contentful/src/index.js index 34b8b795b19f0..e388427494498 100644 --- a/packages/gatsby-remark-images-contentful/src/index.js +++ b/packages/gatsby-remark-images-contentful/src/index.js @@ -70,11 +70,18 @@ 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 (e) { + throw new Error( + `Image downloading failed for ${originalImg}, please check if the image still exists on contentful` + ) + } response.data.pipe(metaReader) From bc6d9f4a03b41dfc51a015860b66638b0461c569 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Sat, 29 Feb 2020 09:25:01 +0100 Subject: [PATCH 2/2] fix tests & use reporter --- .../src/__tests__/index.js | 74 +++++++++++-------- .../src/index.js | 8 +- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/packages/gatsby-remark-images-contentful/src/__tests__/index.js b/packages/gatsby-remark-images-contentful/src/__tests__/index.js index cc5fd1c65830b..2da3628d2395c 100644 --- a/packages/gatsby-remark-images-contentful/src/__tests__/index.js +++ b/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;`), @@ -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, @@ -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 => { @@ -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 () => { @@ -154,13 +159,20 @@ test(`it transforms images with a https scheme in markdown`, async () => { }) 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 expect(plugin(createPluginOptions(content, imagePath))).rejects.toThrow( - `Image downloading failed for ${imagePath}, please check if the image still exists on contentful` + 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) ) }) diff --git a/packages/gatsby-remark-images-contentful/src/index.js b/packages/gatsby-remark-images-contentful/src/index.js index e388427494498..d56d95e61957a 100644 --- a/packages/gatsby-remark-images-contentful/src/index.js +++ b/packages/gatsby-remark-images-contentful/src/index.js @@ -77,10 +77,12 @@ module.exports = async ( url: originalImg, // for some reason there is a './' prefix responseType: `stream`, }) - } catch (e) { - throw new Error( - `Image downloading failed for ${originalImg}, please check if the image still exists on contentful` + } catch (err) { + reporter.panic( + `Image downloading failed for ${originalImg}, please check if the image still exists on contentful`, + err ) + return [] } response.data.pipe(metaReader)