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

Fixed #461 Added support for psr/log context exception #582

Merged
merged 3 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 38 additions & 18 deletions src/DataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Rollbar\Payload\TraceChain;
use Rollbar\Payload\ExceptionInfo;
use Rollbar\Rollbar;
use Stringable;
use Throwable;

class DataBuilder implements DataBuilderInterface
Expand Down Expand Up @@ -341,14 +342,15 @@ protected function setHost($config)
}

/**
* @param string $level
* @param Throwable|string $toLog
* @param $context
* @param string $level
* @param Throwable|string|Stringable $toLog
* @param array $context
*
* @return Data
*/
public function makeData(string $level, Throwable|string $toLog, array $context): Data
public function makeData(string $level, Throwable|string|Stringable $toLog, array $context): Data
{
$env = $this->getEnvironment();
$env = $this->getEnvironment();
$body = $this->getBody($toLog, $context);
$data = new Data($env, $body);
$data->setLevel($this->getLevel($level, $toLog))
Expand All @@ -374,10 +376,19 @@ public function getEnvironment()
return $this->environment;
}

protected function getBody($toLog, $context)
protected function getBody(Throwable|string|Stringable $toLog, array $context): Body
{
$baseException = $this->getBaseException();
if ($toLog instanceof ErrorWrapper) {

// Get the exception from either the $message or $context['exception']. See
// https://www.php-fig.org/psr/psr-3/#13-context for a description of $context['exception'].
if (isset($context['exception']) && $context['exception'] instanceof $baseException) {
$message = null;
if (!$toLog instanceof Throwable) {
$message = (string) $toLog;
}
$content = $this->getExceptionTrace($context['exception'], $message);
} elseif ($toLog instanceof ErrorWrapper) {
$content = $this->getErrorTrace($toLog);
} elseif ($toLog instanceof $baseException) {
$content = $this->getExceptionTrace($toLog);
Expand All @@ -393,13 +404,15 @@ public function getErrorTrace(ErrorWrapper $error)
}

/**
* @param Throwable $exc
* @param Throwable $exc
* @param string|Stringable|null $message
*
* @return Trace|TraceChain
*/
public function getExceptionTrace(Throwable $exc): Trace|TraceChain
public function getExceptionTrace(Throwable $exc, string|Stringable $message = null): Trace|TraceChain
{
$chain = array();
$chain[] = $this->makeTrace($exc, $this->includeExcCodeContext);
$chain = array();
$chain[] = $this->makeTrace($exc, $this->includeExcCodeContext, message: $message);

$previous = $exc->getPrevious();

Expand All @@ -420,22 +433,29 @@ public function getExceptionTrace(Throwable $exc): Trace|TraceChain
}

/**
* @param Throwable $exception
* @param bool $includeContext whether or not to include context
* @param string|null $classOverride
* @param Throwable $exception
* @param bool $includeContext whether or not to include context
* @param string|null $classOverride
* @param string|Stringable|null $message
*
* @return Trace
*/
public function makeTrace(Throwable $exception, bool $includeContext, ?string $classOverride = null): Trace
{
public function makeTrace(
Throwable $exception,
bool $includeContext,
?string $classOverride = null,
string|Stringable $message = null,
): Trace {
if ($this->captureErrorStacktraces) {
$frames = $this->makeFrames($exception, $includeContext);
} else {
$frames = array();
}

$excInfo = new ExceptionInfo(
$classOverride ?: get_class($exception),
$exception->getMessage()
$exception->getMessage(),
$message
);
return new Trace($frames, $excInfo);
}
Expand Down
3 changes: 2 additions & 1 deletion src/DataBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Rollbar;

use Rollbar\Payload\Data;
use Stringable;
use Throwable;

interface DataBuilderInterface
{
public function makeData(string $level, Throwable|string $toLog, array $context): Data;
public function makeData(string $level, Throwable|string|Stringable $toLog, array $context): Data;
}
29 changes: 28 additions & 1 deletion tests/DataBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rollbar;

use Exception;
use Rollbar\Payload\Level;
use Rollbar\TestHelpers\MockPhpStream;

Expand Down Expand Up @@ -686,6 +687,32 @@ public function testFramesWithoutContextDefault(): void
$this->assertNull($output[0]->getContext());
}

public function testExceptionInContext(): void
{
$dataBuilder = new DataBuilder(array(
'accessToken' => $this->getTestAccessToken(),
'environment' => 'tests',
'utilities' => new Utilities(),
));

$output = $dataBuilder->makeData(
Level::ERROR,
"testing",
array(
'exception' => new Exception('testing exception'),
),
)->serialize();

$this->assertSame(
array(
'class' => 'Exception',
'message' => 'testing exception',
'description' => 'testing',
),
$output['body']['trace']['exception'],
);
}

public function testPerson(): void
{
$dataBuilder = new DataBuilder(array(
Expand Down Expand Up @@ -897,7 +924,7 @@ public function testFramesOrder(): void
);
// 893 is the line number where the comment "// A" is found
danielmorell marked this conversation as resolved.
Show resolved Hide resolved
$this->assertEquals(
893,
920,
$frames[count($frames)-1]->getLineno(),
"Possible false negative: did this file change? Check the line number for line with '// A' comment"
);
Expand Down
4 changes: 2 additions & 2 deletions tests/FakeDataBuilder.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php namespace Rollbar;

use Rollbar\DataBuilderInterface;
use Rollbar\Payload\Body;
use Rollbar\Payload\Data;
use Rollbar\Payload\Message;
use Stringable;
use Throwable;

class FakeDataBuilder implements DataBuilderInterface
Expand All @@ -16,7 +16,7 @@ public function __construct($arr)
self::$args[] = $arr;
}

public function makeData(string $level, Throwable|string $toLog, array $context): Data
public function makeData(string $level, Throwable|string|Stringable $toLog, array $context): Data
{
self::$logged[] = array($level, $toLog, $context);

Expand Down