Skip to content

Commit 69c70cf

Browse files
authored
Add error handling for serializing of request headers (#283)
1 parent f6458b2 commit 69c70cf

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/Sensors/RequestSensor.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Laravel\Nightwatch\Concerns\RecordsContext;
88
use Laravel\Nightwatch\Concerns\RedactsHeaders;
99
use Laravel\Nightwatch\ExecutionStage;
10+
use Laravel\Nightwatch\Facades\Nightwatch;
1011
use Laravel\Nightwatch\Records\Request as RequestRecord;
1112
use Laravel\Nightwatch\State\RequestState;
1213
use Laravel\Nightwatch\Types\Str;
@@ -21,6 +22,7 @@
2122
use function is_numeric;
2223
use function is_string;
2324
use function json_encode;
25+
use function rescue;
2426
use function sort;
2527
use function strlen;
2628
use function tap;
@@ -138,7 +140,15 @@ function () use ($record) {
138140
'peak_memory_usage' => $this->requestState->peakMemory(),
139141
'exception_preview' => Str::tinyText($this->requestState->exceptionPreview),
140142
'context' => $this->serializedContext(),
141-
'headers' => Str::text(json_encode((object) $this->redactHeaders($record->headers, $this->redactHeaders)->all(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION)),
143+
'headers' => rescue(
144+
fn () => Str::text(json_encode((object) $this->redactHeaders($record->headers, $this->redactHeaders)->all(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION)),
145+
'{"_nightwatch_error":"Failed to serialize headers"}',
146+
static function ($e) {
147+
Nightwatch::unrecoverableExceptionOccurred($e);
148+
149+
return false;
150+
},
151+
),
142152
];
143153
},
144154
];

0 commit comments

Comments
 (0)