Skip to content

Add package-specific exception hierarchy - #10

Merged
trm42 merged 1 commit into
masterfrom
add-package-exceptions
Jun 12, 2026
Merged

trm42 merged 1 commit into
masterfrom
add-package-exceptions

Conversation

@trm42

@trm42 trm42 commented Jun 12, 2026

Copy link
Copy Markdown
Owner

What

Introduces a small, package-owned exception hierarchy under the new Trm42\CacheDecorator\Exceptions namespace so consumers can catch every cache-decorator-specific failure with a single catch block, while still distinguishing the two concrete failure modes.

  • CacheDecoratorException — abstract class … extends \Exception. Base type for every package error; callers catch this.
  • MissingDecoratedObjectException extends CacheDecoratorException — replaces the LogicException in initDecorated() (no instance passed and decoratedClass() returned null).
  • UndefinedMethodException extends CacheDecoratorException — replaces the BadMethodCallException raised for a forwarded call to a method that exists nowhere on the decorated object.

Why

The package previously threw raw SPL exceptions (LogicException, BadMethodCallException), which made it impossible to catch cache-decorator-specific errors as a group and coupled error handling to generic classes any dependency might also throw.

How

  • initDecorated() now throws MissingDecoratedObjectException (message unchanged).
  • Undefined forwarded calls now surface as UndefinedMethodException. Since calls are forwarded through Laravel's ForwardsCalls trait, this is done by overriding the trait's throwBadMethodCallException() helper — the single point that raised BadMethodCallException — keeping the message (Call to undefined method {Decorator}::{method}()) verbatim so only the thrown type changes.
  • New PSR-4-mapped files under src/Exceptions/ (no composer change needed).
  • Tests updated to assert the concrete subclasses; README documents the new namespace, the base type, when each child is thrown, and a catch (CacheDecoratorException $e) snippet.

Breaking change

The thrown types now extend \Exception via CacheDecoratorException and are not instances of LogicException / BadMethodCallException. Any downstream catch relying on those SPL types must be updated. Worth a release-notes/CHANGELOG line.

Verification

  • composer check green: 31 tests pass, PHPStan reports no errors, Pint passes.

🤖 Generated with Claude Code

Introduce a Trm42\CacheDecorator\Exceptions namespace with an abstract
CacheDecoratorException base so consumers can catch every package error
with a single catch block. The two existing throw sites now use:

- MissingDecoratedObjectException (was LogicException) in initDecorated()
- UndefinedMethodException (was BadMethodCallException) for undefined
  forwarded calls, via an override of the ForwardsCalls trait's
  throwBadMethodCallException(); the message is preserved verbatim.

Update tests and README accordingly. This is a breaking change: the
thrown types are no longer the SPL LogicException / BadMethodCallException.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@trm42
trm42 merged commit e384882 into master Jun 12, 2026
19 checks passed
@trm42
trm42 deleted the add-package-exceptions branch June 12, 2026 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant