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

Merged
merged 5 commits into from
Dec 15, 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`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without this, messages show up in the console looking like: ERROR [object Object] ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like reporter.error broke the old-skool way of sending errors?

context: { sourceMessage: message },
error: err,
})
} else {
console.error(message, err)
}
Expand Down