Skip to content

Commit

Permalink
fix(gatsby-source-contentful): improve error message when dominant co…
Browse files Browse the repository at this point in the history
…lor can't be generated (#31879)

In my application, I was getting the following error logged hundreds of
times:

```
[gatsby-source-contentful] Please install gatsby-plugin-sharp
```

I had gatsby-plugin-sharp installed as a plugin though. I added logging
of the error to the log line and it turns out the error had to do with
`cacheImage` not being able to load an image and not the fact that
gatsby-plugin-sharp was not installed.

This commit moves the `require` of gatsby-plugin-sharp to it's own `try`
block and updates the error messaging of this block to have the error
when an exception is thrown.
  • Loading branch information
eligundry committed Jun 14, 2021
1 parent 315b694 commit 1f89646
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/gatsby-source-contentful/src/extend-node-type.js
Expand Up @@ -698,10 +698,21 @@ exports.extendNodeType = ({ type, store, reporter }) => {
}

const getDominantColor = async ({ image, options }) => {
let pluginSharp

try {
pluginSharp = require(`gatsby-plugin-sharp`)
} catch (e) {
console.error(
`[gatsby-source-contentful] Please install gatsby-plugin-sharp`,
e
)
return `rgba(0,0,0,0.5)`
}

try {
const absolutePath = await cacheImage(store, image, options, reporter)

const pluginSharp = require(`gatsby-plugin-sharp`)
if (!(`getDominantColor` in pluginSharp)) {
console.error(
`[gatsby-source-contentful] Please upgrade gatsby-plugin-sharp`
Expand All @@ -712,7 +723,8 @@ exports.extendNodeType = ({ type, store, reporter }) => {
return pluginSharp.getDominantColor(absolutePath)
} catch (e) {
console.error(
`[gatsby-source-contentful] Please install gatsby-plugin-sharp`
`[gatsby-source-contentful] Could not getDominantColor from image`,
e
)
return `rgba(0,0,0,0.5)`
}
Expand Down Expand Up @@ -810,7 +822,7 @@ exports.extendNodeType = ({ type, store, reporter }) => {
type: ImageLayoutType,
description: stripIndent`
The layout for the image.
CONSTRAINED: Resizes to fit its container, up to a maximum width, at which point it will remain fixed in size.
CONSTRAINED: Resizes to fit its container, up to a maximum width, at which point it will remain fixed in size.
FIXED: A static image size, that does not resize according to the screen width
FULL_WIDTH: The image resizes to fit its container, even if that is larger than the source image.
Pass a value to "sizes" if the container is not the full width of the screen.
Expand Down

0 comments on commit 1f89646

Please sign in to comment.