Skip to content

Commit 61d7e2c

Browse files
Add support for error and exception filtering
1 parent 0c9abd0 commit 61d7e2c

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

src/Flare.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Facade\FlareClient;
44

5+
use Error;
56
use ErrorException;
67
use Exception;
78
use Facade\FlareClient\Concerns\HasContext;
@@ -52,6 +53,12 @@ class Flare
5253
/** @var callable|null */
5354
protected $determineVersionCallable;
5455

56+
/** @var int|null */
57+
protected $reportErrorLevels;
58+
59+
/** @var callable|null */
60+
protected $filterExceptionsCallable;
61+
5562
public static function register(string $apiKey, string $apiSecret = null, ContextDetectorInterface $contextDetector = null, Container $container = null)
5663
{
5764
$client = new Client($apiKey, $apiSecret);
@@ -64,6 +71,16 @@ public function determineVersionUsing($determineVersionCallable)
6471
$this->determineVersionCallable = $determineVersionCallable;
6572
}
6673

74+
public function reportErrorLevels(int $reportErrorLevels)
75+
{
76+
$this->reportErrorLevels = $reportErrorLevels;
77+
}
78+
79+
public function filterExceptionsUsing(callable $filterExceptionsCallable)
80+
{
81+
$this->filterExceptionsCallable = $filterExceptionsCallable;
82+
}
83+
6784
/**
6885
* @return null|string
6986
*/
@@ -175,6 +192,10 @@ public function applicationPath(string $applicationPath)
175192

176193
public function report(Throwable $throwable, callable $callback = null)
177194
{
195+
if (! $this->shouldSendReport($throwable)) {
196+
return;
197+
}
198+
178199
$report = $this->createReport($throwable);
179200

180201
if (! is_null($callback)) {
@@ -184,6 +205,19 @@ public function report(Throwable $throwable, callable $callback = null)
184205
$this->sendReportToApi($report);
185206
}
186207

208+
protected function shouldSendReport(Throwable $throwable): bool
209+
{
210+
if ($this->throwableIsAnError($throwable) && $this->reportErrorLevels) {
211+
return $this->reportErrorLevels & $throwable->getCode();
212+
}
213+
214+
if ($this->filterExceptionsCallable) {
215+
return call_user_func($this->filterExceptionsCallable, $throwable);
216+
}
217+
218+
return true;
219+
}
220+
187221
public function reportMessage(string $message, string $logLevel, callable $callback = null)
188222
{
189223
$report = $this->createReportFromMessage($message, $logLevel);
@@ -276,4 +310,10 @@ protected function applyMiddlewareToReport(Report $report): Report
276310

277311
return $report;
278312
}
313+
314+
315+
protected function throwableIsAnError(Throwable $throwable): bool
316+
{
317+
return $throwable instanceof ErrorException || $throwable instanceof Error;
318+
}
279319
}

tests/FlareTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Facade\FlareClient\Tests;
44

5+
use Error;
56
use Facade\FlareClient\Api;
67
use Facade\FlareClient\Enums\MessageLevels;
78
use Facade\FlareClient\Flare;
89
use Facade\FlareClient\Tests\Concerns\MatchesReportSnapshots;
910
use Facade\FlareClient\Tests\Mocks\FakeClient;
1011
use Facade\FlareClient\Tests\TestClasses\ExceptionWithContext;
1112
use PHPUnit\Framework\Exception;
13+
use Throwable;
1214

1315
class FlareTest extends TestCase
1416
{
@@ -43,6 +45,14 @@ protected function reportException()
4345
$this->flare->report($throwable);
4446
}
4547

48+
protected function reportError(int $code)
49+
{
50+
$throwable = new Error('This is a test', $code);
51+
52+
$this->flare->report($throwable);
53+
}
54+
55+
4656
/** @test */
4757
public function it_can_report_an_exception()
4858
{
@@ -304,4 +314,44 @@ public function it_will_add_the_version_to_the_report()
304314

305315
$this->assertEquals('123', $payload['application_version']);
306316
}
317+
318+
/** @test */
319+
public function it_can_filter_exceptions_being_reported()
320+
{
321+
$this->reportException();
322+
323+
$this->fakeClient->assertRequestsSent(1);
324+
325+
$this->flare->filterExceptionsUsing(function (Throwable $exception) {
326+
return false;
327+
});
328+
329+
$this->reportException();
330+
331+
$this->fakeClient->assertRequestsSent(1);
332+
333+
$this->flare->filterExceptionsUsing(function (Throwable $exception) {
334+
return true;
335+
});
336+
337+
$this->reportException();
338+
339+
$this->fakeClient->assertRequestsSent(2);
340+
}
341+
342+
/** @test */
343+
public function it_can_filter_errors_based_on_their_level()
344+
{
345+
$this->reportError(E_NOTICE);
346+
$this->reportError(E_WARNING);
347+
348+
$this->fakeClient->assertRequestsSent(2);
349+
350+
$this->flare->reportErrorLevels(E_ALL & ~E_NOTICE);
351+
352+
$this->reportError(E_NOTICE);
353+
$this->reportError(E_WARNING);
354+
355+
$this->fakeClient->assertRequestsSent(3);
356+
}
307357
}

0 commit comments

Comments
 (0)