Skip to content

Commit 78ce6bb

Browse files
Merge pull request #23 from facade/error-and-exception-filtering
Add support for error and exception filtering
2 parents 0c9abd0 + 9dcd32d commit 78ce6bb

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

src/Flare.php

Lines changed: 38 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,23 @@ 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->reportErrorLevels && $throwable instanceof Error) {
211+
return $this->reportErrorLevels & $throwable->getCode();
212+
}
213+
214+
if ($this->reportErrorLevels && $throwable instanceof ErrorException) {
215+
return $this->reportErrorLevels & $throwable->getSeverity();
216+
}
217+
218+
if ($this->filterExceptionsCallable && $throwable instanceof Exception) {
219+
return call_user_func($this->filterExceptionsCallable, $throwable);
220+
}
221+
222+
return true;
223+
}
224+
187225
public function reportMessage(string $message, string $logLevel, callable $callback = null)
188226
{
189227
$report = $this->createReportFromMessage($message, $logLevel);

tests/FlareTest.php

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

33
namespace Facade\FlareClient\Tests;
44

5+
use Error;
6+
use ErrorException;
57
use Facade\FlareClient\Api;
68
use Facade\FlareClient\Enums\MessageLevels;
79
use Facade\FlareClient\Flare;
810
use Facade\FlareClient\Tests\Concerns\MatchesReportSnapshots;
911
use Facade\FlareClient\Tests\Mocks\FakeClient;
1012
use Facade\FlareClient\Tests\TestClasses\ExceptionWithContext;
1113
use PHPUnit\Framework\Exception;
14+
use Throwable;
1215

1316
class FlareTest extends TestCase
1417
{
@@ -43,6 +46,14 @@ protected function reportException()
4346
$this->flare->report($throwable);
4447
}
4548

49+
protected function reportError(int $code)
50+
{
51+
$throwable = new Error('This is a test', $code);
52+
53+
$this->flare->report($throwable);
54+
}
55+
56+
4657
/** @test */
4758
public function it_can_report_an_exception()
4859
{
@@ -304,4 +315,60 @@ public function it_will_add_the_version_to_the_report()
304315

305316
$this->assertEquals('123', $payload['application_version']);
306317
}
318+
319+
/** @test */
320+
public function it_can_filter_exceptions_being_reported()
321+
{
322+
$this->reportException();
323+
324+
$this->fakeClient->assertRequestsSent(1);
325+
326+
$this->flare->filterExceptionsUsing(function (Throwable $exception) {
327+
return false;
328+
});
329+
330+
$this->reportException();
331+
332+
$this->fakeClient->assertRequestsSent(1);
333+
334+
$this->flare->filterExceptionsUsing(function (Throwable $exception) {
335+
return true;
336+
});
337+
338+
$this->reportException();
339+
340+
$this->fakeClient->assertRequestsSent(2);
341+
}
342+
343+
/** @test */
344+
public function it_can_filter_errors_based_on_their_level()
345+
{
346+
$this->reportError(E_NOTICE);
347+
$this->reportError(E_WARNING);
348+
349+
$this->fakeClient->assertRequestsSent(2);
350+
351+
$this->flare->reportErrorLevels(E_ALL & ~E_NOTICE);
352+
353+
$this->reportError(E_NOTICE);
354+
$this->reportError(E_WARNING);
355+
356+
$this->fakeClient->assertRequestsSent(3);
357+
}
358+
359+
/** @test */
360+
public function it_can_filter_error_exceptions_based_on_their_severity()
361+
{
362+
$this->flare->report(new ErrorException('test', 0, E_NOTICE));
363+
$this->flare->report(new ErrorException('test', 0, E_WARNING));
364+
365+
$this->fakeClient->assertRequestsSent(2);
366+
367+
$this->flare->reportErrorLevels(E_ALL & ~E_NOTICE);
368+
369+
$this->flare->report(new ErrorException('test', 0, E_NOTICE));
370+
$this->flare->report(new ErrorException('test', 0, E_WARNING));
371+
372+
$this->fakeClient->assertRequestsSent(3);
373+
}
307374
}

0 commit comments

Comments
 (0)