-
-
Notifications
You must be signed in to change notification settings - Fork 27
Add Annotation for Sentry Safe Caller #1047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d8f4b7f
safe caller
addcn-ai-10969 b909671
Update SafeCaller.php
addcn-ai-10969 15d6323
fix(sentry): add null check for SafeCallerAnnotation
addcn-ai-10969 703e960
feat(sentry): 添加 Graceful 注解及其处理逻辑
huangdijia d4ef612
feat(sentry): 移除 SafeCallerAspect,添加 GracefulAspect
huangdijia 513873e
feat(sentry): 移除 SafeCallerAspect 和 SafeCaller 注解
huangdijia 942b47f
fix(sentry): 修正 Graceful 注解的类型提示为 null|Graceful
huangdijia a637c47
fix(sentry): 更新 Graceful 注解中的中文注释为英文
huangdijia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of friendsofhyperf/components. | ||
| * | ||
| * @link https://github.com/friendsofhyperf/components | ||
| * @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
| * @contact huangdijia@gmail.com | ||
| */ | ||
|
|
||
| namespace FriendsOfHyperf\Sentry\Annotation; | ||
|
|
||
| use Attribute; | ||
| use Hyperf\Di\Annotation\AbstractAnnotation; | ||
|
|
||
| #[Attribute(Attribute::TARGET_METHOD)] | ||
| 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'; | ||
|
|
||
| /** | ||
| * 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 Fallback method | ||
| */ | ||
| public function __construct( | ||
| public string $strategy = self::STRATEGY_SWALLOW, | ||
| 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 | ||
| ) { | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of friendsofhyperf/components. | ||
| * | ||
| * @link https://github.com/friendsofhyperf/components | ||
| * @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
| * @contact huangdijia@gmail.com | ||
| */ | ||
|
|
||
| namespace FriendsOfHyperf\Sentry\Aspect; | ||
|
|
||
| use FriendsOfHyperf\Sentry\Annotation\Graceful; | ||
| use Hyperf\Di\Aop\AbstractAspect; | ||
| use Hyperf\Di\Aop\ProceedingJoinPoint; | ||
| use Sentry\SentrySdk; | ||
| use Throwable; | ||
|
|
||
| use function Hyperf\Support\value; | ||
|
|
||
| class GracefulAspect extends AbstractAspect | ||
| { | ||
| public array $annotations = [ | ||
| Graceful::class, | ||
| ]; | ||
|
|
||
| public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed | ||
| { | ||
| $metadata = $proceedingJoinPoint->getAnnotationMetadata(); | ||
|
|
||
| /** @var null|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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.