Skip to content

Commit e557230

Browse files
Use NullSender for Ignition mode; report() takes throwable-or-report
- Ignition mode now swaps in the existing NullSender instead of an Api sendingDisabled flag, so transmission is a no-op with no Api change. - report()'s first argument is a Throwable|ReportFactory union: pass a pre-built report and it is rendered first, then the ignore/filter gating runs. - renderAndReport is inlined: handleException/handleError build the report when a renderer is registered and hand it to report().
1 parent dbaab9d commit e557230

3 files changed

Lines changed: 32 additions & 36 deletions

File tree

src/Api.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public function __construct(
3636
protected Scope $scope,
3737
protected Sender $sender,
3838
protected bool $disableQueue,
39-
protected bool $sendingDisabled = false,
4039
) {
4140
}
4241

@@ -137,10 +136,6 @@ public function sendQueue(): void
137136

138137
protected function sendEntity(FlareEntityType $type, mixed $payload, bool $immediately, bool $test = false): void
139138
{
140-
if ($this->sendingDisabled) {
141-
return;
142-
}
143-
144139
if ($immediately === false && $test === false && $this->disableQueue === false) {
145140
match ($type) {
146141
FlareEntityType::Errors => $this->reportQueue[] = $payload,

src/FlareProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Spatie\FlareClient\Sampling\NeverSampler;
1515
use Spatie\FlareClient\Sampling\Sampler;
1616
use Spatie\FlareClient\Scopes\Scope;
17+
use Spatie\FlareClient\Senders\NullSender;
1718
use Spatie\FlareClient\Senders\Sender;
1819
use Spatie\FlareClient\Spans\Span;
1920
use Spatie\FlareClient\Support\BackTracer;
@@ -59,9 +60,9 @@ public function register(): void
5960
{
6061
$this->container ??= Container::instance();
6162

62-
$this->container->singleton(Sender::class, fn () => new $this->config->sender(
63-
$this->config->senderConfig
64-
));
63+
$this->container->singleton(Sender::class, fn () => $this->mode === FlareMode::Ignition
64+
? new NullSender()
65+
: new $this->config->sender($this->config->senderConfig));
6566

6667
$this->container->singleton(Api::class, fn () => new ($this->config->api)(
6768
apiToken: $this->config->apiToken ?? 'No Api Token provided',
@@ -71,7 +72,6 @@ public function register(): void
7172
resource: $this->container->get(Resource::class),
7273
scope: $this->container->get(Scope::class),
7374
disableQueue: $this->disableApiQueue,
74-
sendingDisabled: $this->mode === FlareMode::Ignition,
7575
));
7676

7777
$this->container->singleton(EntryPointResolver::class, fn () => new EntryPointResolver());

src/Reporter.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,30 @@ public function renderReportsUsing(Closure $renderReportCallable): static
8585
}
8686

8787
/**
88+
* @param Throwable|ReportFactory $throwable A throwable to build a report
89+
* for, or an already built report
90+
* (from the uncaught handler) to
91+
* render and send. When a report is
92+
* passed it is rendered first, then
93+
* the ignore and filter gating runs.
8894
* @param Closure(ReportFactory $report):void|null $callback
89-
* @param ReportFactory|null $report An already built report to send instead
90-
* of building a fresh one. Lets the caller
91-
* build (and render) the report first while
92-
* the ignore and filter gating still runs.
9395
*/
9496
public function report(
95-
Throwable $throwable,
97+
Throwable|ReportFactory $throwable,
9698
?Closure $callback = null,
97-
?bool $handled = null,
98-
?ReportFactory $report = null
99+
?bool $handled = null
99100
): ?ReportFactory {
101+
$report = null;
102+
103+
if ($throwable instanceof ReportFactory) {
104+
$report = $throwable;
105+
$throwable = $report->throwable;
106+
107+
if ($this->renderReportCallable !== null) {
108+
($this->renderReportCallable)($report);
109+
}
110+
}
111+
100112
if (! $this->shouldReport($throwable)) {
101113
$this->tracer->gracefullyEndSpans();
102114

@@ -217,7 +229,12 @@ protected function shouldReport(Throwable $throwable): bool
217229

218230
public function handleException(Throwable $throwable): void
219231
{
220-
$this->renderAndReport($throwable);
232+
// When a renderer is registered, build the report first so it can be
233+
// rendered even for exceptions that are ignored or filtered from being
234+
// sent. report() renders the passed report, then applies the gating.
235+
$this->report(
236+
$this->renderReportCallable !== null ? $this->createReport($throwable) : $throwable
237+
);
221238

222239
if ($this->previousExceptionHandler && is_callable($this->previousExceptionHandler)) {
223240
call_user_func($this->previousExceptionHandler, $throwable);
@@ -228,7 +245,9 @@ public function handleError(mixed $code, string $message, string $file = '', int
228245
{
229246
$exception = new ErrorException($message, 0, $code, $file, $line);
230247

231-
$this->renderAndReport($exception);
248+
$this->report(
249+
$this->renderReportCallable !== null ? $this->createReport($exception) : $exception
250+
);
232251

233252
if ($this->previousErrorHandler) {
234253
call_user_func(
@@ -241,24 +260,6 @@ public function handleError(mixed $code, string $message, string $file = '', int
241260
}
242261
}
243262

244-
protected function renderAndReport(Throwable $throwable): void
245-
{
246-
if ($this->renderReportCallable === null) {
247-
$this->report($throwable);
248-
249-
return;
250-
}
251-
252-
// Build the report once so it can be rendered locally even when the
253-
// exception is ignored or filtered from being sent to Flare. report()
254-
// still runs the ignore and filter gating before sending.
255-
$report = $this->createReport($throwable);
256-
257-
($this->renderReportCallable)($report);
258-
259-
$this->report($throwable, report: $report);
260-
}
261-
262263
protected function addReportToTrace(Throwable $throwable, ?bool $handled, ReportFactory $report): void
263264
{
264265
if ($this->addReportsToTraces === false || $this->tracer->isSampling() === false) {

0 commit comments

Comments
 (0)