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)

Co-authored-by: Matt Kane <[email protected]>
Co-authored-by: gatsbybot <[email protected]>
Co-authored-by: Sidhartha Chatterjee <[email protected]>
  • Loading branch information
4 people authored Dec 15, 2020
1 parent 912f30c commit 02860da
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 02860da

Please sign in to comment.