From d8f4b7f49b22380d4d88e7b57039bb877a1bd66b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E6=B3=BD=E5=AE=8F?= Date: Wed, 17 Dec 2025 18:54:47 +0800 Subject: [PATCH 1/8] safe caller --- src/sentry/src/Annotation/SafeCaller.php | 31 ++++++++++++++ src/sentry/src/Aspect/SafeCallerAspect.php | 50 ++++++++++++++++++++++ src/sentry/src/ConfigProvider.php | 1 + 3 files changed, 82 insertions(+) create mode 100644 src/sentry/src/Annotation/SafeCaller.php create mode 100644 src/sentry/src/Aspect/SafeCallerAspect.php diff --git a/src/sentry/src/Annotation/SafeCaller.php b/src/sentry/src/Annotation/SafeCaller.php new file mode 100644 index 000000000..0125c33e0 --- /dev/null +++ b/src/sentry/src/Annotation/SafeCaller.php @@ -0,0 +1,31 @@ +exceptionHandler = $exceptionHandler; + } +} diff --git a/src/sentry/src/Aspect/SafeCallerAspect.php b/src/sentry/src/Aspect/SafeCallerAspect.php new file mode 100644 index 000000000..92a6254a6 --- /dev/null +++ b/src/sentry/src/Aspect/SafeCallerAspect.php @@ -0,0 +1,50 @@ +getAnnotationMetadata(); + + /** @var SafeCallerAnnotation $safeCaller */ + $safeCaller = $annotation->method[SafeCallerAnnotation::class]; + + try { + return $proceedingJoinPoint->process(); + } catch (Throwable $e) { + $report = true; + + if (is_callable($safeCaller->exceptionHandler)) { + $report = call_user_func($safeCaller->exceptionHandler, $e); + } + + $report && SentrySdk::getCurrentHub()->captureException($e); + + return value($safeCaller->default, $e); + } + } +} diff --git a/src/sentry/src/ConfigProvider.php b/src/sentry/src/ConfigProvider.php index 31a99d4fd..536da5a96 100644 --- a/src/sentry/src/ConfigProvider.php +++ b/src/sentry/src/ConfigProvider.php @@ -20,6 +20,7 @@ public function __invoke(): array return [ 'aspects' => [ Aspect\BreadcrumbAspect::class, + Aspect\SafeCallerAspect::class, Aspect\CacheAspect::class, Aspect\CoroutineAspect::class, Aspect\FilesystemAspect::class, From b909671a6a6b863edb4c74a8a1384d2d6451805d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E6=B3=BD=E5=AE=8F?= Date: Thu, 18 Dec 2025 15:11:00 +0800 Subject: [PATCH 2/8] Update SafeCaller.php --- src/sentry/src/Annotation/SafeCaller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sentry/src/Annotation/SafeCaller.php b/src/sentry/src/Annotation/SafeCaller.php index 0125c33e0..adc3342cc 100644 --- a/src/sentry/src/Annotation/SafeCaller.php +++ b/src/sentry/src/Annotation/SafeCaller.php @@ -21,6 +21,7 @@ class SafeCaller extends AbstractAnnotation /** * @param mixed $default 默认返回值 + * @param null|callable $exceptionHandler 异常处理函数,并且决定是否上报sentry */ public function __construct( public mixed $default = null, From 15d63233ac669816086404551538b18d564862e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E6=B3=BD=E5=AE=8F?= Date: Thu, 18 Dec 2025 15:32:31 +0800 Subject: [PATCH 3/8] fix(sentry): add null check for SafeCallerAnnotation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add null check handling for SafeCallerAnnotation in SafeCallerAspect. If the annotation is not found, proceed with normal execution instead of throwing an error. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/sentry/src/Aspect/SafeCallerAspect.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/sentry/src/Aspect/SafeCallerAspect.php b/src/sentry/src/Aspect/SafeCallerAspect.php index 92a6254a6..1e603b9f8 100644 --- a/src/sentry/src/Aspect/SafeCallerAspect.php +++ b/src/sentry/src/Aspect/SafeCallerAspect.php @@ -31,7 +31,11 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed $annotation = $proceedingJoinPoint->getAnnotationMetadata(); /** @var SafeCallerAnnotation $safeCaller */ - $safeCaller = $annotation->method[SafeCallerAnnotation::class]; + $safeCaller = $annotation->method[SafeCallerAnnotation::class] ?? null; + + if (! $safeCaller) { + return $proceedingJoinPoint->process(); + } try { return $proceedingJoinPoint->process(); From 703e9601f96022cfcff3da805525d09821ab1fb8 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 18 Dec 2025 15:42:58 +0800 Subject: [PATCH 4/8] =?UTF-8?q?feat(sentry):=20=E6=B7=BB=E5=8A=A0=20Gracef?= =?UTF-8?q?ul=20=E6=B3=A8=E8=A7=A3=E5=8F=8A=E5=85=B6=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Annotation/Graceful.php | 50 +++++++++++++++++++++ src/sentry/src/Aspect/GracefulAspect.php | 55 ++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/sentry/src/Annotation/Graceful.php create mode 100644 src/sentry/src/Aspect/GracefulAspect.php diff --git a/src/sentry/src/Annotation/Graceful.php b/src/sentry/src/Annotation/Graceful.php new file mode 100644 index 000000000..de9c44a02 --- /dev/null +++ b/src/sentry/src/Annotation/Graceful.php @@ -0,0 +1,50 @@ +getAnnotationMetadata(); + + /** @var Graceful $annotation */ + $annotation = $metadata->method[Graceful::class] ?? null; + + if ($annotation === null) { + return $proceedingJoinPoint->process(); + } + + try { + return $proceedingJoinPoint->process(); + } catch (Throwable $e) { + return match ($annotation->strategy) { + Graceful::STRATEGY_FALLBACK => value($annotation->fallback, $proceedingJoinPoint, $e), + Graceful::STRATEGY_SWALLOW => null, + Graceful::STRATEGY_RETHROW => throw $e, + Graceful::STRATEGY_TRANSLATE => throw new ($annotation->mapTo ?? Throwable::class)('Translated Exception', 0, $e), + default => null, + }; + } finally { + if (isset($e) && $annotation->report) { + SentrySdk::getCurrentHub()->captureException($e); + } + } + } +} From d4ef6122f4c1cd009dea87e5145171a5fcb7e5f0 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 18 Dec 2025 15:43:41 +0800 Subject: [PATCH 5/8] =?UTF-8?q?feat(sentry):=20=E7=A7=BB=E9=99=A4=20SafeCa?= =?UTF-8?q?llerAspect=EF=BC=8C=E6=B7=BB=E5=8A=A0=20GracefulAspect?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/ConfigProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/ConfigProvider.php b/src/sentry/src/ConfigProvider.php index 536da5a96..ec1a23caf 100644 --- a/src/sentry/src/ConfigProvider.php +++ b/src/sentry/src/ConfigProvider.php @@ -20,10 +20,10 @@ public function __invoke(): array return [ 'aspects' => [ Aspect\BreadcrumbAspect::class, - Aspect\SafeCallerAspect::class, Aspect\CacheAspect::class, Aspect\CoroutineAspect::class, Aspect\FilesystemAspect::class, + Aspect\GracefulAspect::class, Aspect\GuzzleHttpClientAspect::class, Aspect\LoggerAspect::class, Aspect\RedisAspect::class, From 513873ec356ca284fc2535dad14956ed3b08c8c7 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 18 Dec 2025 15:46:30 +0800 Subject: [PATCH 6/8] =?UTF-8?q?feat(sentry):=20=E7=A7=BB=E9=99=A4=20SafeCa?= =?UTF-8?q?llerAspect=20=E5=92=8C=20SafeCaller=20=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Annotation/SafeCaller.php | 32 ------------- src/sentry/src/Aspect/SafeCallerAspect.php | 54 ---------------------- 2 files changed, 86 deletions(-) delete mode 100644 src/sentry/src/Annotation/SafeCaller.php delete mode 100644 src/sentry/src/Aspect/SafeCallerAspect.php diff --git a/src/sentry/src/Annotation/SafeCaller.php b/src/sentry/src/Annotation/SafeCaller.php deleted file mode 100644 index adc3342cc..000000000 --- a/src/sentry/src/Annotation/SafeCaller.php +++ /dev/null @@ -1,32 +0,0 @@ -exceptionHandler = $exceptionHandler; - } -} diff --git a/src/sentry/src/Aspect/SafeCallerAspect.php b/src/sentry/src/Aspect/SafeCallerAspect.php deleted file mode 100644 index 1e603b9f8..000000000 --- a/src/sentry/src/Aspect/SafeCallerAspect.php +++ /dev/null @@ -1,54 +0,0 @@ -getAnnotationMetadata(); - - /** @var SafeCallerAnnotation $safeCaller */ - $safeCaller = $annotation->method[SafeCallerAnnotation::class] ?? null; - - if (! $safeCaller) { - return $proceedingJoinPoint->process(); - } - - try { - return $proceedingJoinPoint->process(); - } catch (Throwable $e) { - $report = true; - - if (is_callable($safeCaller->exceptionHandler)) { - $report = call_user_func($safeCaller->exceptionHandler, $e); - } - - $report && SentrySdk::getCurrentHub()->captureException($e); - - return value($safeCaller->default, $e); - } - } -} From 942b47f1e2e21d2f757e5a1b858387f87eab8147 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 18 Dec 2025 15:46:53 +0800 Subject: [PATCH 7/8] =?UTF-8?q?fix(sentry):=20=E4=BF=AE=E6=AD=A3=20Gracefu?= =?UTF-8?q?l=20=E6=B3=A8=E8=A7=A3=E7=9A=84=E7=B1=BB=E5=9E=8B=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E4=B8=BA=20null|Graceful?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Aspect/GracefulAspect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/Aspect/GracefulAspect.php b/src/sentry/src/Aspect/GracefulAspect.php index e29a99fa2..6ae3311ce 100644 --- a/src/sentry/src/Aspect/GracefulAspect.php +++ b/src/sentry/src/Aspect/GracefulAspect.php @@ -29,7 +29,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed { $metadata = $proceedingJoinPoint->getAnnotationMetadata(); - /** @var Graceful $annotation */ + /** @var null|Graceful $annotation */ $annotation = $metadata->method[Graceful::class] ?? null; if ($annotation === null) { From a637c47f3ae73d3792ce628de3ba5e2de3cf7647 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 18 Dec 2025 15:48:26 +0800 Subject: [PATCH 8/8] =?UTF-8?q?fix(sentry):=20=E6=9B=B4=E6=96=B0=20Gracefu?= =?UTF-8?q?l=20=E6=B3=A8=E8=A7=A3=E4=B8=AD=E7=9A=84=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E4=B8=BA=E8=8B=B1=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Annotation/Graceful.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/sentry/src/Annotation/Graceful.php b/src/sentry/src/Annotation/Graceful.php index de9c44a02..9c078c3e0 100644 --- a/src/sentry/src/Annotation/Graceful.php +++ b/src/sentry/src/Annotation/Graceful.php @@ -18,33 +18,33 @@ class Graceful extends AbstractAnnotation { /** - * 转换为指定异常再抛出. + * Convert to the specified exception and throw it. */ public const STRATEGY_TRANSLATE = 'translate'; /** - * 调用兜底方法并返回. + * Call the fallback method and return. */ public const STRATEGY_FALLBACK = 'fallback'; /** - * 吞掉异常并返回 null. + * Swallow the exception and return null. */ public const STRATEGY_SWALLOW = 'swallow'; /** - * 记录后仍原样抛出. + * Record and rethrow the original exception. */ public const STRATEGY_RETHROW = 'rethrow'; /** - * @param null|callable $fallback 兜底方法 + * @param null|callable $fallback Fallback method */ public function __construct( public string $strategy = self::STRATEGY_SWALLOW, - public $fallback = null, // strategy=fallback 时使用 - public ?string $mapTo = null, // strategy=translate 时使用(异常类全名) - public bool $report = true, // 是否记录日志 + public $fallback = null, // Used when strategy=fallback + public ?string $mapTo = null, // Used when strategy=translate (full exception class name) + public bool $report = true, // Whether to log ) { } }