Add throws() to OptimizerChain for optimizer failure handling - #241
Merged
Merged
Conversation
Optimizer failures (e.g. a binary exiting non-zero) were always logged and swallowed, so callers could not observe a failure, skip the rest of the chain, or abort. Add a fluent `throws()` control on OptimizerChain: - `throws()` / `throws(true)` rethrows the failure and aborts the chain - `throws(false)` keeps the default "log and continue" behaviour - `throws($callable)` runs a custom handler receiving (Throwable $exception, Optimizer $optimizer, Image $image); return to continue, throw to abort Non-zero process exits now raise a ProcessFailedException internally that flows through the same handler. logResult() is kept intact so existing log output and the protected extension point are unchanged. With `throws()` never called the behaviour is identical to before (log and continue), so this is backwards compatible. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
mathiasgrimm
marked this pull request as ready for review
June 25, 2026 18:10
freekmurze
requested changes
Jun 25, 2026
freekmurze
left a comment
Member
There was a problem hiding this comment.
Thanks for the thorough PR and tests.
The P7.3 CI jobs fail with a ParseError because tests/OptimizerChainErrorHandlerTest.php uses arrow functions (fn () => ..., e.g. lines 110, 124, 134, and $logsFor(fn () => null)). Arrow functions require PHP 7.4+, but the package still supports PHP 7.3 ("php": "^7.3|^8.0"). Could you replace those with regular function () { ... } closures so the 7.3 builds pass?
One thing to confirm: with the default handler, a ProcessTimedOutException that previously bubbled out of optimize() is now caught and swallowed. The description notes this is intentional, just want to make sure that's the desired default rather than only changing behaviour once throws() is opted into.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
throws()toOptimizerChainfor optimizer failure handlingProblem
When an optimizer fails (for example its binary exits with a non-zero status),
OptimizerChainlogs the failure and silently continues with the next optimizer. Callers have no way to observe a failure, skip the rest of the chain, or abort the whole optimization. Some failures (such as a SymfonyProcessTimedOutExceptionwhen an optimizer exceeds its timeout) were also thrown straight out ofoptimize()with no way to intercept them.Solution
A single fluent
throws()method onOptimizerChainthat lets you decide what happens on failure:throws()/throws(true): rethrow and abort the chain.throws(false): keep the existing log-and-continue behaviour.throws($callable): custom handler receiving the exception, the optimizer and the image. Return to continue, throw to abort.Internally, a non-zero process exit now raises a
ProcessFailedExceptionthat flows through the same handler, and any exception raised while applying an optimizer (including former timeouts) is routed through it too.Bonus fix
An optimizer's temporary file is now cleaned up even when applying it fails. The cleanup runs in a
finallyblock, so a thrown exception (for example aProcessTimedOutException) no longer leaves the temp file behind. Previously theunlinkhappened after the process ran, so any exception skipped it and leaked the file.Backwards compatibility
Fully backwards compatible:
throws()is never called, behaviour is unchanged: failures are logged and the chain continues.throws(false),throws(true), and both continuing and aborting callbacks. The only difference between modes is whether the chain continues or the exception bubbles.logResult()is kept intact (no removedprotectedmethod, so no break for subclasses).Optimizerinterface or any existing optimizer.Cannot copy fileerror still bubbles up regardless of this setting, since it happens before any optimizer runs.Notes
throws()acceptsbool|callable. Since the package supports PHP 7.3+, where union type hints are unavailable, the argument is validated at runtime and throws anInvalidArgumentExceptionfor anything else.Tests
Adds
tests/OptimizerChainErrorHandlerTest.phpcovering: default-continue,throws()abort,throws(false), continuing and aborting callbacks, theInvalidArgumentExceptionguard, log-line parity across all modes, and that aProcessFailedExceptioncarrying the failed process reaches the handler. Full suite: 32 passing.