Skip to content

Commit 1f89646

Browse files
authored
fix(gatsby-source-contentful): improve error message when dominant color 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.
1 parent 315b694 commit 1f89646

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

packages/gatsby-source-contentful/src/extend-node-type.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,21 @@ exports.extendNodeType = ({ type, store, reporter }) => {
698698
}
699699

700700
const getDominantColor = async ({ image, options }) => {
701+
let pluginSharp
702+
703+
try {
704+
pluginSharp = require(`gatsby-plugin-sharp`)
705+
} catch (e) {
706+
console.error(
707+
`[gatsby-source-contentful] Please install gatsby-plugin-sharp`,
708+
e
709+
)
710+
return `rgba(0,0,0,0.5)`
711+
}
712+
701713
try {
702714
const absolutePath = await cacheImage(store, image, options, reporter)
703715

704-
const pluginSharp = require(`gatsby-plugin-sharp`)
705716
if (!(`getDominantColor` in pluginSharp)) {
706717
console.error(
707718
`[gatsby-source-contentful] Please upgrade gatsby-plugin-sharp`
@@ -712,7 +723,8 @@ exports.extendNodeType = ({ type, store, reporter }) => {
712723
return pluginSharp.getDominantColor(absolutePath)
713724
} catch (e) {
714725
console.error(
715-
`[gatsby-source-contentful] Please install gatsby-plugin-sharp`
726+
`[gatsby-source-contentful] Could not getDominantColor from image`,
727+
e
716728
)
717729
return `rgba(0,0,0,0.5)`
718730
}
@@ -810,7 +822,7 @@ exports.extendNodeType = ({ type, store, reporter }) => {
810822
type: ImageLayoutType,
811823
description: stripIndent`
812824
The layout for the image.
813-
CONSTRAINED: Resizes to fit its container, up to a maximum width, at which point it will remain fixed in size.
825+
CONSTRAINED: Resizes to fit its container, up to a maximum width, at which point it will remain fixed in size.
814826
FIXED: A static image size, that does not resize according to the screen width
815827
FULL_WIDTH: The image resizes to fit its container, even if that is larger than the source image.
816828
Pass a value to "sizes" if the container is not the full width of the screen.

0 commit comments

Comments
 (0)