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

feat(gatsby-plugin-sharp): read plugin options during report error #37165

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/gatsby-plugin-sharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ GATSBY_JPEG_ENCODER=MOZJPEG

### Allow build to continue on image processing error

By default, the build will fail when it encounters an error while processing an image. You can change this so that it continues the build process by setting the plugin option `failOnError` to `false`. Sharp will still throw an error and display it in the console as a GraphQL error, but it will not exit the process. It is important to note that any images that would have otherwise failed will not be accessible via `childImageSharp` until the underlying issue with the image is addressed.
By default, the build will fail when it encounters an error while processing an image. You can change this so that
it continues the build process by setting the plugin option `failOn` to `none`. Sharp will still throw an error and
display it in the console as a GraphQL error, but it will not exit the process. It is important to note that any images that would have otherwise failed will not be accessible via `childImageSharp` until the underlying issue with the image is addressed.

### EXIF and ICC metadata

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions packages/gatsby-plugin-sharp/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ describe(`gatsby-plugin-sharp`, () => {
expect(actual.length).toEqual(expected.length)
expect(actions.createJobV2).toMatchSnapshot()
})

it(`reports metadata error without exiting`, async () => {
process.env.gatsby_executing_command = `build`
setPluginOptions({ failOn: `none` })

const result = await fluid({
file: getFileObject(path.join(__dirname, `images/metadata-error.png`)),
})

expect(result).toBeNull()
})
})

describe(`fixed`, () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ async function stats({ file, reporter }) {

let didShowTraceSVGRemovalWarningFluid = false
async function fluid({ file, args = {}, reporter, cache }) {
const options = healOptions(getPluginOptions(), args, file.extension)
const pluginOptions = getPluginOptions()
const options = healOptions(pluginOptions, args, file.extension)

let metadata
try {
Expand All @@ -429,7 +430,8 @@ async function fluid({ file, args = {}, reporter, cache }) {
reportError(
`Failed to retrieve metadata from image ${file.absolutePath}`,
err,
reporter
reporter,
pluginOptions
)
return null
}
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-sharp/src/plugin-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const generalArgs: Partial<IGeneralArgs> = {

let pluginOptions: ISharpPluginOptions = Object.assign({}, pluginDefaults)
export const setPluginOptions = (
opts: Record<string, string>
opts: Record<string, string | boolean>
): ISharpPluginOptions => {
pluginOptions = Object.assign({}, pluginOptions, opts)
generalArgs.quality = pluginOptions.defaultQuality
Expand Down
8 changes: 5 additions & 3 deletions packages/gatsby-plugin-sharp/src/report-error.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const reportError = (message, err, reporter) => {
const reportError = (message, err, reporter, pluginOptions) => {
if (reporter) {
reporter.error({
id: `gatsby-plugin-sharp-20000`,
Expand All @@ -9,8 +9,10 @@ const reportError = (message, err, reporter) => {
console.error(message, err)
}

if (process.env.gatsby_executing_command === `build`) {
process.exit(1)
if (pluginOptions.failOn !== `none`) {
if (process.env.gatsby_executing_command === `build`) {
process.exit(1)
}
}
}
exports.reportError = reportError