Skip to content
Draft
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
10 changes: 5 additions & 5 deletions src/Concerns/CapturesState.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ public function report(Throwable $e, ?bool $handled = null): void
}

try {
if ($e instanceof FatalError) {
if ($this->sampling) {
$this->ingest->writeNow($this->sensor->fatalError($e));
}
$record = $this->sensor->exception($e, $handled);

if ($this->sampling && ($e instanceof FatalError || ! $record['handled'])) {
$this->ingest->writeNow($record);
} else {
$this->ingest->write($this->sensor->exception($e, $handled));
$this->ingest->write($record);
}
} catch (Throwable $e) {
Nightwatch::unrecoverableExceptionOccurred($e);
Expand Down
35 changes: 0 additions & 35 deletions src/SensorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,13 @@
use Laravel\Nightwatch\Sensors\UserSensor;
use Laravel\Nightwatch\State\CommandState;
use Laravel\Nightwatch\State\RequestState;
use Laravel\Nightwatch\Types\Str;
use Monolog\LogRecord;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\HttpFoundation\Response;
use Throwable;

use function hash;

/**
* @internal
*/
Expand Down Expand Up @@ -252,38 +249,6 @@ public function exception(Throwable $e, ?bool $handled): array
return $sensor($e, $handled);
}

/**
* @return array<mixed>
*/
public function fatalError(Throwable $e): array
{
$file = $this->location->normalizeFile($e->getFile());

return [
'v' => 3,
't' => 'exception',
'timestamp' => $this->clock->microtime(),
'deploy' => $this->executionState->deploy,
'server' => $this->executionState->server,
'_group' => hash('xxh128', $e::class.','.$e->getCode().','.$file.','.$e->getLine()),
'trace_id' => $this->executionState->trace,
'execution_source' => $this->executionState->source,
'execution_id' => '',
'execution_preview' => $this->executionState->executionPreview,
'execution_stage' => $this->executionState->stage,
'user' => $this->executionState->user->resolvedUserId(),
'class' => $e::class,
'file' => Str::tinyText($file),
'line' => $e->getLine(),
'message' => Str::text($e->getMessage()),
'code' => (string) $e->getCode(),
'trace' => '',
'handled' => false,
'php_version' => $this->executionState->phpVersion,
'laravel_version' => $this->executionState->laravelVersion,
];
}

/**
* @return array<mixed>
*/
Expand Down
41 changes: 40 additions & 1 deletion src/Sensors/ExceptionSensor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Spatie\LaravelIgnition\Exceptions\ViewException as IgnitionViewException;
use SplFileObject;
use stdClass;
use Symfony\Component\ErrorHandler\Error\FatalError;
use Throwable;

use function array_is_list;
Expand All @@ -35,6 +36,8 @@
*/
final class ExceptionSensor
{
private const VERSION = 3;

/**
* @var array<string, SplFileObject|null>
*/
Expand All @@ -56,6 +59,10 @@ public function __construct(
*/
public function __invoke(Throwable $e, ?bool $handled): array
{
if ($e instanceof FatalError) {
return $this->fatalError($e);
}

$nowMicrotime = $this->clock->microtime();
[$file, $line] = $this->location->forException($e);
$normalizedException = match ($e->getPrevious()) {
Expand All @@ -76,7 +83,7 @@ public function __invoke(Throwable $e, ?bool $handled): array
$this->executionState->exceptions++;

return [
'v' => 3,
'v' => self::VERSION,
't' => 'exception',
'timestamp' => $nowMicrotime,
'deploy' => $this->executionState->deploy,
Expand All @@ -100,6 +107,38 @@ public function __invoke(Throwable $e, ?bool $handled): array
];
}

/**
* @return array<mixed>
*/
private function fatalError(FatalError $e): array
{
$file = $this->location->normalizeFile($e->getFile());

return [
'v' => self::VERSION,
't' => 'exception',
'timestamp' => $this->clock->microtime(),
'deploy' => $this->executionState->deploy,
'server' => $this->executionState->server,
'_group' => hash('xxh128', $e::class.','.$e->getCode().','.$file.','.$e->getLine()),
'trace_id' => $this->executionState->trace,
'execution_source' => $this->executionState->source,
'execution_id' => '',
'execution_preview' => $this->executionState->executionPreview,
'execution_stage' => $this->executionState->stage,
'user' => $this->executionState->user->resolvedUserId(),
'class' => $e::class,
'file' => Str::tinyText($file),
'line' => $e->getLine(),
'message' => Str::text($e->getMessage()),
'code' => (string) $e->getCode(),
'trace' => '',
'handled' => false,
'php_version' => $this->executionState->phpVersion,
'laravel_version' => $this->executionState->laravelVersion,
];
}

private function wasManuallyReported(Throwable $e): bool
{
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, limit: 20) as $frame) {
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/Hooks/GuzzleMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function test_it_gracefully_handles_exceptions_in_the_before_middleware()
$this->core->sensor->exceptionSensor = function ($e) use (&$exceptions): array {
$exceptions[] = $e;

return [];
return [
'handled' => true,
];
};
$thrownInMicrotimeResolver = false;
$this->core->clock->microtimeResolver = function () use (&$thrownInMicrotimeResolver): float {
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/SamplingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function test_it_samples_on_exception(): void
{
$ingest = $this->fakeIngest();
$this->core->config['sampling']['requests'] = 0;
$this->core->sensor->exceptionSensor = fn () => [];
$this->core->sensor->exceptionSensor = fn () => ['handled' => false];
$exception = new RuntimeException('Whoops!');
Route::get('/users', fn () => throw $exception);

Expand Down
Loading