Lines 100.00% 13 / 13
Methods 100.00% 1 / 1
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __invoke 100.00% 13 / 13 100.00% 1 / 1 6
12final class ExceptionContextProcessor implements ProcessorInterface
13{
14    public const string TRACE_KEY = 'trace';
15
16    public function __invoke(LogRecord $record): LogRecord
17    {
18        $context = $record->context;
19        $forwarded = $context[ExceptionDetails::CONTEXT_KEY] ?? null;
20        $wasForwarded = \array_key_exists(ExceptionDetails::CONTEXT_KEY, $context);
21        unset($context[ExceptionDetails::CONTEXT_KEY]);
22
23        $exception = $context['exception'] ?? null;
24        $details = $forwarded instanceof ExceptionDetails
25            ? $forwarded
26            : ($exception instanceof Throwable ? ExceptionDetails::fromThrowable($exception) : null);
27
28        if ($details !== null) {
29            return $record->with(context: $context, extra: [...$record->extra, 'error' => $details->toArray()]);
30        }
31
32        // A Stewart exception is an expected failure; anything else is a bug that needs its trace.
33        if ($exception instanceof Throwable) {
34            return $record->with(context: $context, extra: [...$record->extra, self::TRACE_KEY => $exception->getTraceAsString()]);
35        }
36
37        return $wasForwarded ? $record->with(context: $context) : $record;
38    }
39}