From 435fd4cac9078490655a664af3660d04f744d094 Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Sat, 14 Nov 2026 19:42:08 +0000 Subject: [PATCH] ensure recording failure does not break the request --- src/BarstoolServiceProvider.php | 54 ++++++++++++++++++++------------- tests/BarstoolTest.php | 29 ++++++++++++++++++ 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/src/BarstoolServiceProvider.php b/src/BarstoolServiceProvider.php index 3f0a78e..c2a643b 100644 --- a/src/BarstoolServiceProvider.php +++ b/src/BarstoolServiceProvider.php @@ -4,6 +4,7 @@ namespace Saloon\Barstool; +use Throwable; use Saloon\Config; use Saloon\Http\Response; use Saloon\Enums\PipeOrder; @@ -31,36 +32,47 @@ public function packageRegistered(): void { Config::globalMiddleware() ->onFatalException(function (FatalRequestException $exception) { - if (Barstool::shouldRecord($exception) === false) { - return; - } - - Barstool::record($exception); + try { + if (Barstool::shouldRecord($exception) === false) { + return; + } + Barstool::record($exception); + } catch (Throwable $recordingException) { + report($recordingException); + } }, order: PipeOrder::FIRST) ->onRequest(function (PendingRequest $request) { - if (Barstool::shouldRecord($request) === false) { - return; - } + try { + if (Barstool::shouldRecord($request) === false) { + return; + } - $request->config()->add( - 'barstool-request-time', - microtime(true) * 1000 - ); + $request->config()->add( + 'barstool-request-time', + microtime(true) * 1000 + ); - Barstool::record($request); + Barstool::record($request); + } catch (Throwable $recordingException) { + report($recordingException); + } }) ->onResponse(function (Response $response) { - if (Barstool::shouldRecord($response) === false) { - return; - } + try { + if (Barstool::shouldRecord($response) === false) { + return; + } - $response->getPendingRequest()->config()->add( - 'barstool-response-time', - microtime(true) * 1000 - ); + $response->getPendingRequest()->config()->add( + 'barstool-response-time', + microtime(true) * 1000 + ); - Barstool::record($response); + Barstool::record($response); + } catch (Throwable $recordingException) { + report($recordingException); + } }); } } diff --git a/tests/BarstoolTest.php b/tests/BarstoolTest.php index 97152e8..cc4a964 100644 --- a/tests/BarstoolTest.php +++ b/tests/BarstoolTest.php @@ -1260,3 +1260,32 @@ expect(BarstoolRecorder::calculateDuration($pendingRequest))->toBe(1); }); + +it('does not fail the api call when a recording cannot be stored', function () { + config()->set('barstool.enabled', true); + Schema::drop('barstools'); + + MockClient::global([ + SoloUserRequest::class => MockResponse::make( + body: ['data' => 'ok'], + status: 200, + ), + ]); + + $response = (new SoloUserRequest)->send(); + + expect($response->status())->toBe(200); + expect($response->json())->toBe(['data' => 'ok']); +}); + +it('does not replace a connection error when a recording cannot be stored', function () { + config()->set('barstool.enabled', true); + Schema::drop('barstools'); + + $pendingRequest = (new SoloUserRequest)->createPendingRequest(); + $connectionError = new FatalRequestException(new RuntimeException('connection refused'), $pendingRequest); + + $pendingRequest->executeFatalPipeline($connectionError); + + expect($connectionError->getMessage())->toBe('connection refused'); +});