Skip to content

Commit 13609db

Browse files
xuanyanwow何泽宏claudehuangdijia
authored
Add Annotation for Sentry Safe Caller (#1047)
* safe caller * Update SafeCaller.php * fix(sentry): add null check for SafeCallerAnnotation 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 <[email protected]> * feat(sentry): 添加 Graceful 注解及其处理逻辑 * feat(sentry): 移除 SafeCallerAspect,添加 GracefulAspect * feat(sentry): 移除 SafeCallerAspect 和 SafeCaller 注解 * fix(sentry): 修正 Graceful 注解的类型提示为 null|Graceful * fix(sentry): 更新 Graceful 注解中的中文注释为英文 --------- Co-authored-by: 何泽宏 <[email protected]> Co-authored-by: Claude <[email protected]> Co-authored-by: Deeka Wong <[email protected]>
1 parent ec76f5b commit 13609db

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of friendsofhyperf/components.
6+
*
7+
* @link https://github.com/friendsofhyperf/components
8+
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
9+
* @contact [email protected]
10+
*/
11+
12+
namespace FriendsOfHyperf\Sentry\Annotation;
13+
14+
use Attribute;
15+
use Hyperf\Di\Annotation\AbstractAnnotation;
16+
17+
#[Attribute(Attribute::TARGET_METHOD)]
18+
class Graceful extends AbstractAnnotation
19+
{
20+
/**
21+
* Convert to the specified exception and throw it.
22+
*/
23+
public const STRATEGY_TRANSLATE = 'translate';
24+
25+
/**
26+
* Call the fallback method and return.
27+
*/
28+
public const STRATEGY_FALLBACK = 'fallback';
29+
30+
/**
31+
* Swallow the exception and return null.
32+
*/
33+
public const STRATEGY_SWALLOW = 'swallow';
34+
35+
/**
36+
* Record and rethrow the original exception.
37+
*/
38+
public const STRATEGY_RETHROW = 'rethrow';
39+
40+
/**
41+
* @param null|callable $fallback Fallback method
42+
*/
43+
public function __construct(
44+
public string $strategy = self::STRATEGY_SWALLOW,
45+
public $fallback = null, // Used when strategy=fallback
46+
public ?string $mapTo = null, // Used when strategy=translate (full exception class name)
47+
public bool $report = true, // Whether to log
48+
) {
49+
}
50+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of friendsofhyperf/components.
6+
*
7+
* @link https://github.com/friendsofhyperf/components
8+
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
9+
* @contact [email protected]
10+
*/
11+
12+
namespace FriendsOfHyperf\Sentry\Aspect;
13+
14+
use FriendsOfHyperf\Sentry\Annotation\Graceful;
15+
use Hyperf\Di\Aop\AbstractAspect;
16+
use Hyperf\Di\Aop\ProceedingJoinPoint;
17+
use Sentry\SentrySdk;
18+
use Throwable;
19+
20+
use function Hyperf\Support\value;
21+
22+
class GracefulAspect extends AbstractAspect
23+
{
24+
public array $annotations = [
25+
Graceful::class,
26+
];
27+
28+
public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed
29+
{
30+
$metadata = $proceedingJoinPoint->getAnnotationMetadata();
31+
32+
/** @var null|Graceful $annotation */
33+
$annotation = $metadata->method[Graceful::class] ?? null;
34+
35+
if ($annotation === null) {
36+
return $proceedingJoinPoint->process();
37+
}
38+
39+
try {
40+
return $proceedingJoinPoint->process();
41+
} catch (Throwable $e) {
42+
return match ($annotation->strategy) {
43+
Graceful::STRATEGY_FALLBACK => value($annotation->fallback, $proceedingJoinPoint, $e),
44+
Graceful::STRATEGY_SWALLOW => null,
45+
Graceful::STRATEGY_RETHROW => throw $e,
46+
Graceful::STRATEGY_TRANSLATE => throw new ($annotation->mapTo ?? Throwable::class)('Translated Exception', 0, $e),
47+
default => null,
48+
};
49+
} finally {
50+
if (isset($e) && $annotation->report) {
51+
SentrySdk::getCurrentHub()->captureException($e);
52+
}
53+
}
54+
}
55+
}

src/sentry/src/ConfigProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __invoke(): array
2323
Aspect\CacheAspect::class,
2424
Aspect\CoroutineAspect::class,
2525
Aspect\FilesystemAspect::class,
26+
Aspect\GracefulAspect::class,
2627
Aspect\GuzzleHttpClientAspect::class,
2728
Aspect\LoggerAspect::class,
2829
Aspect\RedisAspect::class,

0 commit comments

Comments
 (0)