@@ -116,7 +116,7 @@ composer require okapi/aop
116116 target classes. Aspects must be annotated with the ` #[Aspect] ` attribute.
117117
118118- ** Advice** : The logic that you want to apply to your target classes. Advice
119- methods must be annotated with the ` #[Before] ` , ` #[Around] ` or ` #[After ] `
119+ methods must be annotated with ` #[Before] ` , ` #[Around] ` , ` #[After] ` , or ` #[AfterThrowing ] `
120120 attributes.
121121
122122- ** Join Point** : A point in the execution of your target classes where you can
@@ -182,7 +182,7 @@ use Okapi\Aop\Invocation\AfterMethodInvocation;
182182class DiscountAspect
183183{
184184 // Annotate the methods that you want to intercept with
185- // "Before", "Around" or "After " attributes
185+ // "Before", "Around", "After", or "AfterThrowing " attributes
186186 #[After(
187187 // Use named arguments
188188 // You can also use Wildcards (see Okapi/Wildcards package)
@@ -307,6 +307,47 @@ class PaymentProcessorAspect
307307```
308308
309309
310+ ### AfterThrowing advice
311+
312+ ` #[AfterThrowing] ` runs when the intercepted target method throws an exception or
313+ PHP error (` Throwable ` ). It receives the original throwable and the invocation
314+ context:
315+
316+ ``` php
317+ use Okapi\Aop\Attributes\AfterThrowing;
318+ use Okapi\Aop\Invocation\AfterThrowingMethodInvocation;
319+
320+ // Inside an aspect:
321+ #[AfterThrowing(
322+ class: PaymentProcessor::class,
323+ method: 'processPayment',
324+ exception: \DomainException::class,
325+ )]
326+ public function logFailure(AfterThrowingMethodInvocation $invocation): void
327+ {
328+ error_log($invocation->getThrowable()->getMessage());
329+ }
330+ ```
331+
332+ It supports the same class/method patterns, ` order ` , ` interceptTraitMethods ` , and
333+ ` onlyPublicMethods ` options as the other method advice types, including explicit
334+ class-level and method-level aspects. Advice runs in ascending ` order ` .
335+
336+ The optional ` exception ` filter matches the given type and its subclasses. It
337+ defaults to ` Throwable::class ` , which includes exceptions and PHP errors. Use
338+ ` Exception::class ` to exclude errors, or ` Error::class ` to handle only errors.
339+ An unknown type or a type that does not implement ` Throwable ` is rejected when the
340+ advice attribute is instantiated.
341+
342+ - Successful calls and exceptions caught inside the target do not trigger it.
343+ - The original throwable is rethrown after the advice completes. Return values are
344+ ignored; the invocation has no ` proceed() ` or ` setResult() ` method.
345+ - It runs before an outer ` Around ` advice catches a target failure. Repeated target
346+ calls through ` proceed(true) ` trigger it for each failure.
347+ - Exceptions originating in ` Before ` , ` Around ` , or ` After ` advice do not trigger it.
348+ - If an ` AfterThrowing ` advice throws, that new throwable propagates and remaining
349+ ` AfterThrowing ` advice is skipped.
350+
310351### Regular-expression patterns
311352
312353` class ` and ` method ` accept either wildcard strings or explicit
@@ -324,7 +365,7 @@ use Okapi\Wildcards\Regex;
324365
325366This matches controller methods whose names start with a letter, excluding
326367constructors and other magic methods. Regex objects can also be used for ` class ` .
327- The same arguments are supported by ` Before ` , ` Around ` , and ` After ` .
368+ The same arguments are supported by ` Before ` , ` Around ` , ` After ` , and ` AfterThrowing ` .
328369
329370Pass a complete PHP regular expression, including delimiters and any modifiers.
330371Regex patterns are used as written: add ` ^ ` and ` $ ` when you want to match the
@@ -709,7 +750,7 @@ $firstLog = $logs[0];
709750
710751# Features
711752
712- - ** Advice types:** "Before", "Around" and "After "
753+ - ** Advice types:** "Before", "Around", "After", and "AfterThrowing "
713754
714755- Intercept "private" and "protected" methods
715756 (Will show errors in IDEs)
0 commit comments