Skip to content

Commit

Permalink
fix(gatsby-plugin-sharp): catch errors when writing base64 images (#2…
Browse files Browse the repository at this point in the history
…8614) (#28659)

Co-authored-by: Matt Kane <matt@gatsbyjs.com>
Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
Co-authored-by: Sidhartha Chatterjee <me@sidharthachatterjee.com>
(cherry picked from commit 02860da)

Co-authored-by: Kyle Mathews <mathews.kyle@gmail.com>
  • Loading branch information
LekoArts and KyleAMathews committed Dec 16, 2020
1 parent b5b96bc commit ea741c3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
21 changes: 18 additions & 3 deletions packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,24 @@ async function generateBase64({ file, args = {}, reporter }) {
if (options.duotone) {
pipeline = await duotone(options.duotone, options.toFormat, pipeline)
}
const { data: buffer, info } = await pipeline.toBuffer({
resolveWithObject: true,
})
let buffer
let info
try {
const result = await pipeline.toBuffer({
resolveWithObject: true,
})
buffer = result.data
info = result.info
} catch (err) {
reportError(
`Failed to process image ${file.absolutePath}.
It is probably corrupt, so please try replacing it. If it still fails, please open an issue with the image attached.`,
err,
reporter
)
return null
}

const base64output = {
src: `data:image/${info.format};base64,${buffer.toString(`base64`)}`,
width: info.width,
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby-plugin-sharp/src/report-error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const reportError = (message, err, reporter) => {
if (reporter) {
reporter.error(message, err)
reporter.error({
id: `gatsby-plugin-sharp-20000`,
context: { sourceMessage: message },
error: err,
})
} else {
console.error(message, err)
}
Expand Down

0 comments on commit ea741c3

Please sign in to comment.