Skip to content

Commit

Permalink
Allow formatted message to be passed in event payload (fixes getsentr…
Browse files Browse the repository at this point in the history
…y#750); use mb_substr (refs getsentry#727).
  • Loading branch information
mfb committed Jan 31, 2019
1 parent c1136a4 commit d787b5a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ final class Event implements \JsonSerializable
*/
private $message;

/**
* @var string|null The formatted error message
*/
private $messageFormatted;

/**
* @var array The parameters to use to format the message
*/
Expand Down Expand Up @@ -339,6 +344,16 @@ public function getMessage(): ?string
return $this->message;
}

/**
* Gets the formatted message.
*
* @return string|null
*/
public function getMessageFormatted(): ?string
{
return $this->messageFormatted;
}

/**
* Gets the parameters to use to format the message.
*
Expand All @@ -352,13 +367,15 @@ public function getMessageParams(): array
/**
* Sets the error message.
*
* @param string $message The message
* @param array $params The parameters to use to format the message
* @param string $message The message
* @param array $params The parameters to use to format the message
* @param string|null $formatted The formatted message
*/
public function setMessage(string $message, array $params = []): void
public function setMessage(string $message, array $params = [], string $formatted = null): void
{
$this->message = $message;
$this->messageParams = $params;
$this->messageFormatted = $formatted;
}

/**
Expand Down Expand Up @@ -659,7 +676,7 @@ public function toArray(): array
$data['message'] = [
'message' => $this->message,
'params' => $this->messageParams,
'formatted' => vsprintf($this->message, $this->messageParams),
'formatted' => $this->messageFormatted ?? vsprintf($this->message, $this->messageParams),
];
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ public function create(array $payload): Event

$message = $payload['message'] ?? null;
$messageParams = $payload['message_params'] ?? [];
$messageFormatted = isset($payload['message_formatted']) ? mb_substr($payload['message_formatted'], 0, $this->options->getMaxValueLength()) : null;

if (null !== $message) {
$event->setMessage(substr($message, 0, $this->options->getMaxValueLength()), $messageParams);
$event->setMessage(mb_substr($message, 0, $this->options->getMaxValueLength()), $messageParams, $messageFormatted);
}

if (isset($payload['exception']) && $payload['exception'] instanceof \Throwable) {
Expand Down
14 changes: 14 additions & 0 deletions tests/EventFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public function createWithPayloadDataProvider()
],
],
],
[
[
'message' => 'testMessage @param',
'message_params' => ['@param' => 'param'],
'message_formatted' => 'testMessage param',
],
[
'message' => [
'message' => 'testMessage @param',
'params' => ['@param' => 'param'],
'formatted' => 'testMessage param',
],
],
],
];
}

Expand Down
32 changes: 32 additions & 0 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,38 @@ public function testToArrayWithBreadcrumbs(): void
$this->assertSame($breadcrumbs, $data['breadcrumbs']['values']);
}

public function testToArrayWithMessageWithFormatted(): void
{
$expected = [
'message' => 'foo @bar',
'params' => ['@bar' => 'bar'],
'formatted' => 'foo bar',
];

$event = new Event();
$event->setMessage('foo @bar', ['@bar' => 'bar'], 'foo bar');

$data = $event->toArray();

$this->assertArrayHasKey('message', $data);
$this->assertSame($expected, $data['message']);
}

public function testGetMessage(): void
{
$event = new Event();
$event->setMessage('foo @bar', ['@bar' => 'bar'], 'foo bar');
$this->assertSame('foo @bar', $event->getMessage());
$this->assertSame(['@bar' => 'bar'], $event->getMessageParams());
$this->assertSame('foo bar', $event->getMessageFormatted());

$event = new Event();
$event->setMessage('foo %s', ['bar']);
$this->assertSame('foo %s', $event->getMessage());
$this->assertSame(['bar'], $event->getMessageParams());
$this->assertNull($event->getMessageFormatted());
}

public function testGetServerOsContext(): void
{
$event = new Event();
Expand Down

0 comments on commit d787b5a

Please # to comment.