Skip to content

Commit 02860da

Browse files
KyleAMathewsascorbicgatsbybotsidharthachatterjee
authored
fix(gatsby-plugin-sharp): catch errors when writing base64 images (#28614)
Co-authored-by: Matt Kane <[email protected]> Co-authored-by: gatsbybot <[email protected]> Co-authored-by: Sidhartha Chatterjee <[email protected]>
1 parent 912f30c commit 02860da

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

packages/gatsby-plugin-sharp/src/index.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,24 @@ async function generateBase64({ file, args = {}, reporter }) {
331331
if (options.duotone) {
332332
pipeline = await duotone(options.duotone, options.toFormat, pipeline)
333333
}
334-
const { data: buffer, info } = await pipeline.toBuffer({
335-
resolveWithObject: true,
336-
})
334+
let buffer
335+
let info
336+
try {
337+
const result = await pipeline.toBuffer({
338+
resolveWithObject: true,
339+
})
340+
buffer = result.data
341+
info = result.info
342+
} catch (err) {
343+
reportError(
344+
`Failed to process image ${file.absolutePath}.
345+
It is probably corrupt, so please try replacing it. If it still fails, please open an issue with the image attached.`,
346+
err,
347+
reporter
348+
)
349+
return null
350+
}
351+
337352
const base64output = {
338353
src: `data:image/${info.format};base64,${buffer.toString(`base64`)}`,
339354
width: info.width,

packages/gatsby-plugin-sharp/src/report-error.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const reportError = (message, err, reporter) => {
22
if (reporter) {
3-
reporter.error(message, err)
3+
reporter.error({
4+
id: `gatsby-plugin-sharp-20000`,
5+
context: { sourceMessage: message },
6+
error: err,
7+
})
48
} else {
59
console.error(message, err)
610
}

0 commit comments

Comments
 (0)