Skip to content

Commit

Permalink
Trying...
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jun 28, 2021
1 parent 999d9c6 commit ac7acfe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
13 changes: 12 additions & 1 deletion src/Middleware/HandleErrorsWithSentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,30 @@

namespace FoF\Sentry\Middleware;

use Illuminate\Container\Container;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class HandleErrorsWithSentry implements MiddlewareInterface
{
/**
* @var Container
*/
public $container;

public function __construct(Container $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
app()->instance('sentry.request', $request);
$this->container->instance('sentry.request', $request);

return $handler->handle($request);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Reporters/SentryReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FoF\Sentry\Reporters;

use Flarum\Foundation\ErrorHandling\Reporter;
use Flarum\Http\RequestUtil;
use Illuminate\Contracts\Container\Container;
use Psr\Log\LoggerInterface;
use Sentry\State\HubInterface;
Expand All @@ -37,9 +38,10 @@ public function __construct(LoggerInterface $logger, Container $container)

public function report(Throwable $error)
{
//dd($error);
/** @var HubInterface $hub */
$hub = $this->container->make('sentry');

//dd($hub);
if ($hub === null) {
$this->logger->warning('[fof/sentry] sentry dsn not set');

Expand All @@ -49,7 +51,7 @@ public function report(Throwable $error)
if ($this->container->bound('sentry.request')) {
$hub->configureScope(function (Scope $scope) {
$request = $this->container->make('sentry.request');
$user = $request->getAttribute('actor');
$user = RequestUtil::getActor($request);

if ($user && $user->id !== 0) {
$scope->setUser([
Expand Down
13 changes: 8 additions & 5 deletions src/SentryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function register()

if ($dsn && $performanceMonitoring > 0) {
/** @var HubInterface $hub */
$hub = resolve(HubInterface::class);
$hub = $this->container->make(HubInterface::class);

$transaction = $hub->startTransaction(new TransactionContext('flarum'));

Expand All @@ -90,15 +90,15 @@ public function register()
$config = $this->container->make('flarum.config');

/** @var HubInterface $hub */
$hub = resolve(HubInterface::class);
$hub = $this->container->make(HubInterface::class);

$hub->configureScope(function (Scope $scope) use ($config) {
$scope->setTag('offline', (int) Arr::get($config, 'offline', false));
$scope->setTag('debug', (int) Arr::get($config, 'debug', true));
$scope->setTag('flarum', Application::VERSION);

if ($this->container->bound('sentry.stack')) {
$scope->setTag('stack', resolve('sentry.stack'));
$scope->setTag('stack', $this->container->make('sentry.stack'));
}
});

Expand Down Expand Up @@ -138,12 +138,12 @@ function (Assets $assets) {
protected function init(string $dsn, int $performanceMonitoring)
{
/** @var Paths $paths */
$paths = resolve(Paths::class);
$paths = $this->container->make(Paths::class);

$tracesSampleRate = $performanceMonitoring > 0 ? round($performanceMonitoring / 100, 2) : 0;

/** @var Config $config */
$config = resolve(Config::class);
$config = $this->container->make(Config::class);

init([
'dsn' => $dsn,
Expand All @@ -168,6 +168,9 @@ public function handleError($level, $message, $file = '', $line = 0)
throw $error;
} else {
foreach ($this->container->tagged(Reporter::class) as $reporter) {
/**
* @var SentryReporter $reporter
*/
$reporter->report($error);
}
}
Expand Down

0 comments on commit ac7acfe

Please sign in to comment.