Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only skip real exceptions #1781

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
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
24 changes: 13 additions & 11 deletions src/Monolog/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function doWrite($record): void

$hint = new EventHint();

if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
if ($this->hasExceptionContext($record['context'])) {
$hint->exception = $record['context']['exception'];
}

Expand Down Expand Up @@ -94,18 +94,12 @@ private function getMonologContextData(array $context): array
return [];
}

$contextData = [];

foreach ($context as $key => $value) {
// We skip the `exception` field because it goes in its own context
if ($key === self::CONTEXT_EXCEPTION_KEY) {
continue;
}

$contextData[$key] = $value;
if ($this->hasExceptionContext($context)) {
// remove the exception from the context, as it's set on the hint
unset($context[self::CONTEXT_EXCEPTION_KEY]);
}

return $contextData;
return $context;
}

/**
Expand All @@ -127,4 +121,12 @@ private function getMonologExtraData(array $context): array

return $extraData;
}

/**
* @param mixed[] $context
*/
private function hasExceptionContext(array $context): bool
{
return isset($context[self::CONTEXT_EXCEPTION_KEY]) && $context[self::CONTEXT_EXCEPTION_KEY] instanceof \Throwable;
}
}
2 changes: 2 additions & 0 deletions tests/Monolog/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public static function handleDataProvider(): iterable
[
'foo' => 'bar',
'bar' => 'baz',
'exception' => 'just a message',
],
[]
),
Expand All @@ -280,6 +281,7 @@ public static function handleDataProvider(): iterable
'monolog.context' => [
'foo' => 'bar',
'bar' => 'baz',
'exception' => 'just a message',
],
],
];
Expand Down
Loading