Skip to content

Commit

Permalink
Make it possible to overwrite serializer string limit of 1024 (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
brainexe authored and Jean85 committed Apr 20, 2018
1 parent 625d5e5 commit b2b6a4e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
36 changes: 33 additions & 3 deletions lib/Raven/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,26 @@ class Raven_Serializer
*/
protected $mb_detect_order = self::DEFAULT_MB_DETECT_ORDER;

/**
* The default maximum message lengths. Longer strings will be truncated
*
* @var int
*/
protected $message_limit = Raven_Client::MESSAGE_LIMIT;

/**
* @param null|string $mb_detect_order
* @param null|int $message_limit
*/
public function __construct($mb_detect_order = null)
public function __construct($mb_detect_order = null, $message_limit = null)
{
if ($mb_detect_order != null) {
$this->mb_detect_order = $mb_detect_order;
}

if ($message_limit != null) {
$this->message_limit = (int) $message_limit;
}
}

/**
Expand Down Expand Up @@ -93,8 +105,8 @@ protected function serializeString($value)
}
}

if (strlen($value) > 1024) {
$value = substr($value, 0, 1014) . ' {clipped}';
if (strlen($value) > $this->message_limit) {
$value = substr($value, 0, $this->message_limit - 10) . ' {clipped}';
}

return $value;
Expand Down Expand Up @@ -141,4 +153,22 @@ public function setMbDetectOrder($mb_detect_order)

return $this;
}

/**
* @return int
* @codeCoverageIgnore
*/
public function getMessageLimit()
{
return $this->message_limit;
}

/**
* @param int $message_limit
* @codeCoverageIgnore
*/
public function setMessageLimit($message_limit)
{
$this->message_limit = (int)$message_limit;
}
}
20 changes: 20 additions & 0 deletions test/Raven/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ public function testLongString()
}
}

/**
* @covers Raven_Serializer::serializeString
*/
public function testLongStringWithOverwrittenMessageLength()
{
$serializer = new Raven_Serializer();
$serializer->setMessageLimit(500);
for ($i = 0; $i < 100; $i++) {
foreach (array(100, 490, 499, 500, 501, 1000, 10000) as $length) {
$input = '';
for ($i = 0; $i < $length; $i++) {
$input .= chr(mt_rand(0, 255));
}
$result = $serializer->serialize($input);
$this->assertInternalType('string', $result);
$this->assertLessThanOrEqual(500, strlen($result));
}
}
}

/**
* @covers Raven_Serializer::serializeValue
*/
Expand Down

0 comments on commit b2b6a4e

Please sign in to comment.