Skip to content

Commit ccd625f

Browse files
authored
Gracefully handle unexpected log output (#66)
* Gracefully handle unexpected log output * Code style
1 parent ca923f7 commit ccd625f

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

src/Results/SettledResult.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Illuminate\Contracts\Support\Responsable;
1414
use Illuminate\Support\Arr;
1515
use Illuminate\Support\Str;
16+
use Throwable;
1617

1718
class SettledResult implements Responsable, ResultContract
1819
{
@@ -211,29 +212,33 @@ protected function parseLogs()
211212
$lines = explode("\n", $lines);
212213

213214
$lines = array_map(function ($line) use (&$reportLineReached) {
214-
if ($reportLineReached) {
215-
return null;
216-
}
215+
try {
216+
if ($reportLineReached) {
217+
return null;
218+
}
217219

218-
if (Str::startsWith($line, 'START RequestId:')) {
219-
return $this->parseStartLine($line);
220-
}
220+
if (Str::startsWith($line, 'START RequestId:')) {
221+
return $this->parseStartLine($line);
222+
}
221223

222-
if (Str::startsWith($line, 'END RequestId:')) {
223-
return null;
224-
}
224+
if (Str::startsWith($line, 'END RequestId:')) {
225+
return null;
226+
}
225227

226-
if (Str::startsWith($line, 'REPORT RequestId')) {
227-
$reportLineReached = true;
228+
if (Str::startsWith($line, 'REPORT RequestId')) {
229+
$reportLineReached = true;
228230

229-
return $this->parseReportLine($line);
230-
}
231+
return $this->parseReportLine($line);
232+
}
231233

232-
if ($line === '') {
233-
return null;
234-
}
234+
if ($line === '') {
235+
return null;
236+
}
235237

236-
return $this->parseInfoLine($line);
238+
return $this->parseInfoLine($line);
239+
} catch (Throwable $exception) {
240+
return $this->unknownLine($line);
241+
}
237242
}, $lines);
238243

239244
return array_values(array_filter($lines));
@@ -251,12 +256,8 @@ protected function parseInfoLine($line)
251256
{
252257
$parts = explode("\t", $line);
253258

254-
if (count($parts) === 1) {
255-
return [
256-
'timestamp' => now()->timestamp,
257-
'level' => 'UNKNOWN',
258-
'body' => $parts[0]
259-
];
259+
if (count($parts) < 4) {
260+
return $this->unknownLine($line);
260261
}
261262

262263
$body = $parts[3];
@@ -272,6 +273,15 @@ protected function parseInfoLine($line)
272273
];
273274
}
274275

276+
protected function unknownLine($line)
277+
{
278+
return [
279+
'timestamp' => now()->timestamp,
280+
'level' => 'UNKNOWN',
281+
'body' => $line,
282+
];
283+
}
284+
275285
protected function parseReportLine($line)
276286
{
277287
$parts = array_filter(explode("\t", $line));

0 commit comments

Comments
 (0)