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

[2.0] Merge up to 1.9 #592

Merged
merged 20 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@

- ...

## 1.9.0 (2018-05-03)

- Fixed undefined variable (#588)
- Fix for exceptions throwing exceptions when setting event id (#587)
- Fix monolog handler not accepting Throwable (#586)
- Add `excluded_exceptions` option to exclude exceptions and their extending exceptions (#583)
- Fix `HTTP_X_FORWARDED_PROTO` header detection (#578)
- Fix sending events async in PHP 5 (#576)
- Avoid double reporting due to `ErrorException`s (#574)
- Make it possible to overwrite serializer message limit of 1024 (#559)
- Allow request data to be nested up to 5 levels deep (#554)
- Update serializer to handle UTF-8 characters correctly (#553)

## 1.8.4 (2018-03-20)

- Revert ignoring fatal errors on PHP 7+ (#571)
- Add PHP runtime information (#564)
- Cleanup the `site` value if it's empty (#555)
- Add `application/json` input handling (#546)

## 1.8.3 (2018-02-07)

- Serialize breadcrumbs to prevent issues with binary data (#538)
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
# Sentry for PHP

[![Build Status](https://secure.travis-ci.org/getsentry/sentry-php.png?branch=master)](http://travis-ci.org/getsentry/sentry-php)
[![Total Downloads](https://img.shields.io/packagist/dt/sentry/sentry.svg?style=flat-square)](https://packagist.org/packages/sentry/sentry)
[![Downloads per month](https://img.shields.io/packagist/dm/sentry/sentry.svg?style=flat-square)](https://packagist.org/packages/sentry/sentry)
[![Latest stable version](https://img.shields.io/packagist/v/sentry/sentry.svg?style=flat-square)](https://packagist.org/packages/sentry/sentry)
[![License](http://img.shields.io/packagist/l/sentry/sentry.svg?style=flat-square)](https://packagist.org/packages/sentry/sentry)
[![Total Downloads](https://poser.pugx.org/sentry/sentry/downloads)](https://packagist.org/packages/sentry/sentry)
[![Monthly Downloads](https://poser.pugx.org/sentry/sentry/d/monthly)](https://packagist.org/packages/sentry/sentry)
[![Latest Stable Version](https://poser.pugx.org/sentry/sentry/v/stable)](https://packagist.org/packages/sentry/sentry)
[![License](https://poser.pugx.org/sentry/sentry/license)](https://packagist.org/packages/sentry/sentry)
[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/getsentry/sentry-php/master.svg)](https://scrutinizer-ci.com/g/getsentry/sentry-php/)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/getsentry/sentry-php/master.svg)](https://scrutinizer-ci.com/g/getsentry/sentry-php/)

Expand Down Expand Up @@ -131,7 +131,7 @@ $ git checkout -b releases/1.9.x

3. Update the hardcoded version tag in ``Client.php``:

```
```php
class Raven_Client
{
const VERSION = '1.9.0';
Expand Down Expand Up @@ -170,7 +170,7 @@ git checkout master

9. Update the version in ``Client.php``:

```
```php
class Raven_Client
{
const VERSION = '1.10.x-dev';
Expand All @@ -179,7 +179,7 @@ class Raven_Client

10. Lastly, update the composer version in ``composer.json``:

```
```json
"extra": {
"branch-alias": {
"dev-master": "1.10.x-dev"
Expand Down
22 changes: 21 additions & 1 deletion docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ The following settings are available for the client:
'User-Agent' => $client->getUserAgent(),
'X-Sentry-Auth' => $client->getAuthHeader(),
),
'body' => gzipCompress(jsonEncode($data)),
'body' => gzcompress(jsonEncode($data)),
))
},

Expand Down Expand Up @@ -241,6 +241,26 @@ The following settings are available for the client:
)
)

.. describe:: timeout

The timeout for sending requests to the Sentry server in seconds, default is 2 seconds.

.. code-block:: php

'timeout' => 2,

.. describe:: excluded_exceptions

Exception that should not be reported, exceptions extending exceptions in this list will also
be excluded, default is an empty array.

In the example below, when you exclude ``LogicException`` you will also exclude ``BadFunctionCallException``
since it extends ``LogicException``.

.. code-block:: php

'excluded_exceptions' => array('LogicException'),

.. _sentry-php-request-context:

Providing Request Context
Expand Down
2 changes: 2 additions & 0 deletions docs/integrations/laravel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ step is not required anymore an you can skip ahead to the next one:

<?php

use Symfony\Component\HttpKernel\Exception\HttpException;

class Handler extends ExceptionHandler
{
public function report(Exception $exception)
Expand Down
6 changes: 3 additions & 3 deletions docs/sentry-doc-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"type": "framework",
"doc_link": "integrations/laravel/",
"wizard": [
"index#installation",
"integrations/laravel#laravel-5-x"
"integrations/laravel#laravel-5-x",
"integrations/laravel#laravel-4-x",
"integrations/laravel#lumen-5-x"
]
},
"php.monolog": {
Expand All @@ -34,7 +35,6 @@
"type": "framework",
"doc_link": "integrations/symfony2/",
"wizard": [
"index#installation",
"integrations/symfony2#symfony-2"
]
}
Expand Down
10 changes: 8 additions & 2 deletions lib/Raven/Breadcrumbs/MonologHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ protected function write(array $record)
return;
}

if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) {
if (
isset($record['context']['exception'])
&& (
$record['context']['exception'] instanceof \Exception
|| (PHP_VERSION_ID >= 70000 && $record['context']['exception'] instanceof \Throwable)
)
) {
/**
* @var \Exception
* @var \Exception|\Throwable
*/
$exc = $record['context']['exception'];

Expand Down
4 changes: 2 additions & 2 deletions lib/Raven/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* Raven PHP Client.
*
* @doc https://docs.sentry.io/clients/php/config/
* @see https://docs.sentry.io/clients/php/config/
*/
class Client
{
Expand Down Expand Up @@ -471,7 +471,7 @@ public function sanitize(Event $event)
$tagsContext = $event->getTagsContext();

if (!empty($request)) {
$event = $event->withRequest($this->serializer->serialize($request));
$event = $event->withRequest($this->serializer->serialize($request, 5));
}
if (!empty($userContext)) {
$event = $event->withUserContext($this->serializer->serialize($userContext, 3));
Expand Down
43 changes: 36 additions & 7 deletions lib/Raven/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,24 @@ class Serializer
*/
protected $_all_object_serialize = false;

/**
* The default maximum message lengths. Longer strings will be truncated.
*
* @var int
*/
protected $messageLimit;

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

$this->messageLimit = (int) $messageLimit;
}

/**
Expand Down Expand Up @@ -122,19 +132,22 @@ public function serializeObject($object, $max_depth = 3, $_depth = 0, $hashes =
protected function serializeString($value)
{
$value = (string) $value;
if (function_exists('mb_detect_encoding')
&& function_exists('mb_convert_encoding')
) {

if (extension_loaded('mbstring')) {
// we always guarantee this is coerced, even if we can't detect encoding
if ($currentEncoding = mb_detect_encoding($value, $this->mb_detect_order)) {
$value = mb_convert_encoding($value, 'UTF-8', $currentEncoding);
} else {
$value = mb_convert_encoding($value, 'UTF-8');
}
}

if (strlen($value) > 1024) {
$value = substr($value, 0, 1014) . ' {clipped}';
if (mb_strlen($value) > $this->messageLimit) {
$value = mb_substr($value, 0, $this->messageLimit - 10, 'UTF-8') . ' {clipped}';
}
} else {
if (strlen($value) > $this->messageLimit) {
$value = substr($value, 0, $this->messageLimit - 10) . ' {clipped}';
}
}

return $value;
Expand Down Expand Up @@ -197,4 +210,20 @@ public function getAllObjectSerialize()
{
return $this->_all_object_serialize;
}

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

/**
* @param int $messageLimit
*/
public function setMessageLimit($messageLimit)
{
$this->messageLimit = $messageLimit;
}
}
107 changes: 82 additions & 25 deletions tests/Breadcrumbs/MonologHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Raven\Tests\Breadcrumbs;

use Monolog\Logger;
use ParseError;
use PHPUnit\Framework\TestCase;
use Raven\Breadcrumbs\Breadcrumb;
use Raven\Breadcrumbs\MonologHandler;
Expand Down Expand Up @@ -44,52 +45,108 @@ protected function getSampleErrorMessage()

public function testSimple()
{
$client = $client = ClientBuilder::create([
'install_default_breadcrumb_handlers' => false,
])->getClient();

$handler = new MonologHandler($client);
$client = $this->createClient();
$logger = $this->createLoggerWithHandler($client);

$logger = new Logger('sentry');
$logger->pushHandler($handler);
$logger->addWarning('foo');

$breadcrumbsRecorder = $this->getObjectAttribute($client, 'breadcrumbRecorder');
$breadcrumbs = $this->getBreadcrumbs($client);
$this->assertCount(1, $breadcrumbs);
$this->assertEquals('foo', $breadcrumbs[0]->getMessage());
$this->assertEquals(Client::LEVEL_WARNING, $breadcrumbs[0]->getLevel());
$this->assertEquals('sentry', $breadcrumbs[0]->getCategory());
}

/** @var \Raven\Breadcrumbs\Breadcrumb[] $breadcrumbs */
$breadcrumbs = iterator_to_array($breadcrumbsRecorder);
public function testErrorInMessage()
{
$client = $this->createClient();
$logger = $this->createLoggerWithHandler($client);

$logger->addError($this->getSampleErrorMessage());

$breadcrumbs = $this->getBreadcrumbs($client);
$this->assertCount(1, $breadcrumbs);
$this->assertEquals(Breadcrumb::TYPE_ERROR, $breadcrumbs[0]->getType());
$this->assertEquals(Client::LEVEL_ERROR, $breadcrumbs[0]->getLevel());
$this->assertEquals('sentry', $breadcrumbs[0]->getCategory());
$this->assertEquals('An unhandled exception', $breadcrumbs[0]->getMetadata()['value']);
}

public function testExceptionBeingParsed()
{
$client = $this->createClient();
$logger = $this->createLoggerWithHandler($client);

$this->assertEquals($breadcrumbs[0]->getMessage(), 'foo');
$this->assertEquals($breadcrumbs[0]->getLevel(), Client::LEVEL_WARNING);
$this->assertEquals($breadcrumbs[0]->getCategory(), 'sentry');
$logger->addError('A message', ['exception' => new \Exception('Foo bar')]);

$breadcrumbs = $this->getBreadcrumbs($client);
$this->assertCount(1, $breadcrumbs);
$this->assertEquals(Breadcrumb::TYPE_ERROR, $breadcrumbs[0]->getType());
$this->assertEquals('Foo bar', $breadcrumbs[0]->getMetadata()['value']);
$this->assertEquals('sentry', $breadcrumbs[0]->getCategory());
$this->assertEquals(Client::LEVEL_ERROR, $breadcrumbs[0]->getLevel());
$this->assertNull($breadcrumbs[0]->getMessage());
}

public function testErrorInMessage()
public function testThrowableBeingParsedAsException()
{
if (PHP_VERSION_ID <= 70000) {
$this->markTestSkipped('PHP 7.0 introduced Throwable');
}

$client = $this->createClient();
$logger = $this->createLoggerWithHandler($client);
$throwable = new ParseError('Foo bar');

$logger->addError('This is a throwable', ['exception' => $throwable]);

$breadcrumbs = $this->getBreadcrumbs($client);
$this->assertCount(1, $breadcrumbs);
$this->assertEquals(Breadcrumb::TYPE_ERROR, $breadcrumbs[0]->getType());
$this->assertEquals('Foo bar', $breadcrumbs[0]->getMetadata()['value']);
$this->assertEquals('sentry', $breadcrumbs[0]->getCategory());
$this->assertEquals(Client::LEVEL_ERROR, $breadcrumbs[0]->getLevel());
$this->assertNull($breadcrumbs[0]->getMessage());
}

/**
* @return Client
*/
private function createClient()
{
$client = $client = ClientBuilder::create([
'install_default_breadcrumb_handlers' => false,
])->getClient();

$handler = new MonologHandler($client);
return $client;
}

/**
* @param Client $client
*
* @return Logger
*/
private function createLoggerWithHandler(Client $client)
{
$handler = new MonologHandler($client);
$logger = new Logger('sentry');
$logger->pushHandler($handler);
$logger->addError($this->getSampleErrorMessage());

return $logger;
}

/**
* @param Client $client
*
* @return Breadcrumb[]
*/
private function getBreadcrumbs(Client $client)
{
$breadcrumbsRecorder = $this->getObjectAttribute($client, 'breadcrumbRecorder');

/** @var \Raven\Breadcrumbs\Breadcrumb[] $breadcrumbs */
$breadcrumbs = iterator_to_array($breadcrumbsRecorder);
$this->assertContainsOnlyInstancesOf(Breadcrumb::class, $breadcrumbs);

$this->assertCount(1, $breadcrumbs);

$metaData = $breadcrumbs[0]->getMetadata();

$this->assertEquals($breadcrumbs[0]->getType(), Breadcrumb::TYPE_ERROR);
$this->assertEquals($breadcrumbs[0]->getLevel(), Client::LEVEL_ERROR);
$this->assertEquals($breadcrumbs[0]->getCategory(), 'sentry');
$this->assertEquals($metaData['value'], 'An unhandled exception');
return $breadcrumbs;
}
}
Loading