Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions src/BarstoolServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Saloon\Barstool;

use Throwable;
use Saloon\Config;
use Saloon\Http\Response;
use Saloon\Enums\PipeOrder;
Expand Down Expand Up @@ -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);
}
});
}
}
29 changes: 29 additions & 0 deletions tests/BarstoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Loading