Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/sentry/src/Annotation/Graceful.php
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
) {
}
}
55 changes: 55 additions & 0 deletions src/sentry/src/Aspect/GracefulAspect.php
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);
}
}
}
}
1 change: 1 addition & 0 deletions src/sentry/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __invoke(): array
Aspect\CacheAspect::class,
Aspect\CoroutineAspect::class,
Aspect\FilesystemAspect::class,
Aspect\GracefulAspect::class,
Aspect\GuzzleHttpClientAspect::class,
Aspect\LoggerAspect::class,
Aspect\RedisAspect::class,
Expand Down
Loading