Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-sharp): catch errors when writing base64 images (#28614) #28659

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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