22
33namespace Facade \FlareClient ;
44
5+ use Error ;
56use ErrorException ;
67use Exception ;
78use 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}
0 commit comments