Skip to content

Commit

Permalink
Handle undefined routes
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored May 15, 2024
1 parent 51c2268 commit 0865090
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Recorders/ValidationErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ public function register(callable $record, Application $app): void
*/
public function record(LivewireValidationError|RequestHandled $event): void
{
$this->pulse->lazy(function () use ($event) {
if (! $this->shouldSample()) {
return;
}
if (
$event->request->route() === null ||
! $this->shouldSample()
) {
return;
}

$this->pulse->lazy(function () use ($event) {
[$path, $via] = $this->resolveRoutePath($event->request);

if ($this->shouldIgnore($path)) {
Expand Down
25 changes: 25 additions & 0 deletions tests/RecorderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Tests\TestClasses\DummyComponent;
use TiMacDonald\Pulse\Recorders\ValidationErrors;

use function Pest\Laravel\get;
use function Pest\Laravel\post;
use function Pest\Laravel\postJson;

Expand Down Expand Up @@ -483,3 +484,27 @@
expect($aggregates->pluck('aggregate')->all())->toBe(array_fill(0, 4, 'count'));
expect($aggregates->pluck('value')->every(fn ($value) => $value == 1.0))->toBe(true);
});

it('ignores unknown routes', function () {
get('unknown-route')->assertNotFound();
});

it('can sample', function () {
Config::set('pulse.recorders.'.ValidationErrors::class.'.sample_rate', 0.1);
Route::post('users', fn () => Request::validate([
'email' => 'required',
]))->middleware('web');

post('users');
post('users');
post('users');
post('users');
post('users');
post('users');
post('users');
post('users');
post('users');
post('users');

expect(Pulse::ingest())->toEqualWithDelta(1, 4);
});

0 comments on commit 0865090

Please sign in to comment.