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 27, 2019
1 parent a13e23c commit 2b81119
Show file tree
Hide file tree
Showing 2 changed files with 23 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, Client::MESSAGE_MAX_LENGTH_LIMIT) : null;

if (null !== $message) {
$event->setMessage(substr($message, 0, Client::MESSAGE_MAX_LENGTH_LIMIT), $messageParams);
$event->setMessage(mb_substr($message, 0, Client::MESSAGE_MAX_LENGTH_LIMIT), $messageParams, $messageFormatted);
}

if (isset($payload['exception']) && $payload['exception'] instanceof \Throwable) {
Expand Down

0 comments on commit 2b81119

Please # to comment.