Repository: getsentry/sentry-php (php)

Issue:
Do not register the error handler if the DSN is null
<!--
Please provide some context on what problem you are trying to solve and how this feature is going to help.
-->

Related to https://github.com/getsentry/sentry-symfony/issues/46#issuecomment-780003387

> it should be possible to change the way the client is built during the container compilation so that if the DSN is null then we skip adding those integrations to the client.

Pull request description:
ref:	Do not register the error handler if the DSN is null
Closes #1190

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 01dfaff..637113d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 
 ## Unreleased
 
+- Do not setup any error handlers if the DSN is null (#1349)
 - Add setter for type on the `ExceptionDataBag` (#1347)
 - Drop symfony/polyfill-uuid in favour of a standalone implementation (#1346)
 
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..095a381
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,106 @@
+<p align="center">
+  <a href="https://sentry.io/?utm_source=github&utm_medium=logo" target="_blank">
+    <img src="https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png" alt="Sentry" width="280" height="84">
+  </a>
+</p>
+
+# Contributing to the Sentry PHP SDK
+
+We welcome contributions to `sentry-php` by the community.
+
+Please search the [issue tracker](https://github.com/getsentry/sentry-php/issues) before creating a new issue (a problem or an improvement request). Please also ask in our [Sentry Community on Discord](https://discord.com/invite/Ww9hbqr) before submitting a new issue. There is a ton of great people in our Discord community ready to help you!
+
+If you feel that you can fix or implement it yourself, please read on to learn how to submit your changes.
+
+## Submitting changes
+
+- Setup the development environment.
+- Clone the `sentry-php` repository and prepare necessary changes.
+- Add tests for your changes to `tests/`.
+- Run tests and make sure all of them pass.
+- Submit a pull request, targeting the `develop` branch if you added any new features. For bug fixes of the current release, please target the `master` branch instead.
+- Make sure to update the `CHANGELOG.md` file below the `Unreleased` heading.
+
+We will review your pull request as soon as possible.
+Thank you for contributing!
+
+## Development environment
+
+### Clone the repository
+
+```bash
+git clone git@github.com:getsentry/sentry-php.git
+```
+
+Make sure that you have PHP 7.2+ installed. Version 7.4 or higher is required to run style checkers. On macOS, we recommend using brew to install PHP. For Windows, we recommend an official PHP.net release.
+
+### Install the dependencies
+
+Dependencies are managed through [Composer](https://getcomposer.org/):
+
+```bash
+composer install
+```
+
+### Running tests
+
+Tests can then be run via [PHPUnit](https://phpunit.de/):
+
+```bash
+vendor/bin/phpunit
+```
+
+## Releasing a new version
+
+(only relevant for Sentry employees)
+
+Prerequisites:
+
+- All changes that should be released must be in the `master` branch.
+- Every commit should follow the [Commit Message Format](https://develop.sentry.dev/commit-messages/#commit-message-format) convention.
+
+Manual Process:
+
+- Update CHANGELOG.md with the version to be released. Example commit: https://github.com/getsentry/sentry-php/commit/877bca3f0f0ac0fc8ec0a218c6070cccea266795.
+- On GitHub in the `sentry-php` repository go to "Actions" select the "Release" workflow.
+- Click on "Run workflow" on the right side and make sure the `master` branch is selected.
+- Set "Version to release" input field. Here you decide if it is a major, minor or patch release. (See "Versioning Policy" below)
+- Click "Run Workflow"
+
+This will trigger [Craft](https://github.com/getsentry/craft) to prepare everything needed for a release. (For more information see [craft prepare](https://github.com/getsentry/craft#craft-prepare-preparing-a-new-release)) At the end of this process, a release issue is created in the [Publish](https://github.com/getsentry/publish) repository. (Example release issue: https://github.com/getsentry/publish/issues/815)
+
+Now one of the persons with release privileges (most probably your engineering manager) will review this Issue and then add the `accepted` label to the issue.
+
+There are always two persons involved in a release.
+
+If you are in a hurry and the release should be out immediately there is a Slack channel called `#proj-release-approval` where you can see your release issue and where you can ping people to please have a look immediately.
+
+When the release issue is labeled `accepted` [Craft](https://github.com/getsentry/craft) is triggered again to publish the release to all the right platforms. (See [craft publish](https://github.com/getsentry/craft#craft-publish-publishing-the-release) for more information). At the end of this process, the release issue on GitHub will be closed and the release is completed! Congratulations!
+
+There is a sequence diagram visualizing all this in the [README.md](https://github.com/getsentry/publish) of the `Publish` repository.
+
+### Versioning Policy
+
+This project follows [semver](https://semver.org/), with three additions:
+
+- Semver says that major version `0` can include breaking changes at any time. Still, it is common practice to assume that only `0.x` releases (minor versions) can contain breaking changes while `0.x.y` releases (patch versions) are used for backwards-compatible changes (bugfixes and features). This project also follows that practice.
+
+- All undocumented APIs are considered internal. They are not part of this contract.
+
+- Certain features (e.g. integrations) may be explicitly called out as "experimental" or "unstable" in the documentation. They come with their own versioning policy described in the documentation.
+
+We recommend pinning your version requirements against `1.x.*` or `1.x.y`.
+Either one of the following is fine:
+
+```json
+"sentry/sentry": "^1.0",
+"sentry/sentry": "^1",
+```
+
+A major release `N` implies the previous release `N-1` will no longer receive updates. We generally do not backport bugfixes to older versions unless they are security relevant. However, feel free to ask for backports of specific commits on the bug tracker.
+
+## Commit message format guidelines
+
+See the documentation on commit messages here:
+
+https://develop.sentry.dev/commit-messages/#commit-message-format
diff --git a/README.md b/README.md
index 874a099..1735a46 100644
--- a/README.md
+++ b/README.md
@@ -6,12 +6,12 @@
 
 _Bad software is everywhere, and we're tired of it. Sentry is on a mission to help developers write better software faster, so we can get back to enjoying technology. If you want to join us [<kbd>**Check out our open positions**</kbd>](https://sentry.io/careers/)_
 
-# Sentry SDK for PHP
+# Offical Sentry SDK for PHP
 
-[![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)
+[![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)
 [![Discord](https://img.shields.io/discord/621778831602221064)](https://discord.gg/cWnMQeA)
 
 | Version | Build Status | Code Coverage |
@@ -23,7 +23,9 @@ The Sentry PHP error reporter tracks errors and exceptions that happen during th
 execution of your application and provides instant notification with detailed
 information needed to prioritize, identify, reproduce and fix each issue.
 
-## Install
+## Getting started
+
+### Install
 
 To install the SDK you will need to be using [Composer]([https://getcomposer.org/)
 in your project. To install it please see the [docs](https://getcomposer.org/download/).
@@ -54,11 +56,15 @@ you want to use because it's an implementation detail of your application. You m
 use any package that provides [`php-http/async-client-implementation`](https://packagist.org/providers/php-http/async-client-implementation)
 and [`http-message-implementation`](https://packagist.org/providers/psr/http-message-implementation).
 
-## Usage
+### Configuration
 
 ```php
 \Sentry\init(['dsn' => '___PUBLIC_DSN___' ]);
+```
 
+### Usage
+
+```php
 try {
     thisFunctionThrows(); // -> throw new \Exception('foo bar');
 } catch (\Exception $exception) {
@@ -66,14 +72,14 @@ try {
 }
 ```
 
-### Official integrations
+## Official integrations
 
 The following integrations are fully supported and maintained by the Sentry team.
 
 - [Symfony](https://github.com/getsentry/sentry-symfony)
 - [Laravel](https://github.com/getsentry/sentry-laravel)
 
-### 3rd party integrations
+## 3rd party integrations
 
 The following integrations are available and maintained by members of the Sentry community.
 
@@ -86,14 +92,14 @@ The following integrations are available and maintained by members of the Sentry
 - [CakePHP](https://github.com/Connehito/cake-sentry)
 - ... feel free to be famous, create a port to your favourite platform!
 
-### 3rd party integrations using old SDK 2.x
+## 3rd party integrations using old SDK 2.x
 
 - [Neos Flow](https://github.com/networkteam/Networkteam.SentryClient)
 - [OXID eShop](https://github.com/OXIDprojects/sentry)
 - [TYPO3](https://github.com/networkteam/sentry_client)
 - [CakePHP](https://github.com/Connehito/cake-sentry/tree/3.x)
 
-### 3rd party integrations using old SDK 1.x
+## 3rd party integrations using old SDK 1.x
 
 - [Neos CMS](https://github.com/networkteam/Netwokteam.Neos.SentryClient)
 - [OpenCart](https://github.com/BurdaPraha/oc_sentry)
@@ -105,19 +111,24 @@ The following integrations are available and maintained by members of the Sentry
 - [Bug Tracker](http://github.com/getsentry/sentry-php/issues)
 - [Code](http://github.com/getsentry/sentry-php)
 
-## Contributing
+## Contributing to the SDK
 
-Dependencies are managed through `composer`:
+Please refer to [CONTRIBUTING.md](CONTRIBUTING.md).
 
-```
-$ composer install
-```
+## Getting help/support
 
-Tests can then be run via phpunit:
+If you need help setting up or configuring the PHP SDK (or anything else in the Sentry universe) please head over to the [Sentry Community on Discord](https://discord.com/invite/Ww9hbqr). There is a ton of great people in our Discord community ready to help you!
 
-```
-$ vendor/bin/phpunit
-```
+## Resources
+
+- [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/quickstart/)
+- [![Discord](https://img.shields.io/discord/621778831602221064)](https://discord.gg/Ww9hbqr)
+- [![Stack Overflow](https://img.shields.io/badge/stack%20overflow-sentry-green.svg)](http://stackoverflow.com/questions/tagged/sentry)
+- [![Twitter Follow](https://img.shields.io/twitter/follow/getsentry?label=getsentry&style=social)](https://twitter.com/intent/follow?screen_name=getsentry)
+
+## License
+
+Licensed under the BSD license, see [`LICENSE`](LICENSE)
 
 [master Build Status]: https://github.com/getsentry/sentry-php/actions?query=workflow%3ACI+branch%3Amaster
 [master Build Status Image]: https://github.com/getsentry/sentry-php/workflows/CI/badge.svg?branch=master
diff --git a/src/Integration/IntegrationRegistry.php b/src/Integration/IntegrationRegistry.php
index ff15b2b..f117c43 100644
--- a/src/Integration/IntegrationRegistry.php
+++ b/src/Integration/IntegrationRegistry.php
@@ -126,14 +126,17 @@ private function getDefaultIntegrations(Options $options): array
             return [];
         }
 
-        return [
-            new ExceptionListenerIntegration(),
-            new ErrorListenerIntegration(),
-            new FatalErrorListenerIntegration(),
+        $integrations = [
             new RequestIntegration(),
             new TransactionIntegration(),
             new FrameContextifierIntegration(),
             new EnvironmentIntegration(),
         ];
+
+        if (null !== $options->getDsn()) {
+            array_unshift($integrations, new ExceptionListenerIntegration(), new ErrorListenerIntegration(), new FatalErrorListenerIntegration());
+        }
+
+        return $integrations;
     }
 }


Test patch:
diff --git a/tests/Integration/IntegrationRegistryTest.php b/tests/Integration/IntegrationRegistryTest.php
index d25ba99..259565b 100644
--- a/tests/Integration/IntegrationRegistryTest.php
+++ b/tests/Integration/IntegrationRegistryTest.php
@@ -67,6 +67,7 @@ public function setupOnce(): void
 
         yield 'No default integrations and no user integrations' => [
             new Options([
+                'dsn' => 'http://public@example.com/sentry/1',
                 'default_integrations' => false,
             ]),
             [],
@@ -75,6 +76,7 @@ public function setupOnce(): void
 
         yield 'Default integrations and no user integrations' => [
             new Options([
+                'dsn' => 'http://public@example.com/sentry/1',
                 'default_integrations' => true,
             ]),
             [
@@ -117,6 +119,7 @@ public function setupOnce(): void
 
         yield 'Default integrations and some user integrations' => [
             new Options([
+                'dsn' => 'http://public@example.com/sentry/1',
                 'default_integrations' => true,
                 'integrations' => [
                     $integration1,
@@ -149,6 +152,7 @@ public function setupOnce(): void
 
         yield 'Default integrations and some user integrations, one of which is also a default integration' => [
             new Options([
+                'dsn' => 'http://public@example.com/sentry/1',
                 'default_integrations' => true,
                 'integrations' => [
                     new TransactionIntegration(),
@@ -206,6 +210,7 @@ public function setupOnce(): void
 
         yield 'Default integrations and a callable as user integrations' => [
             new Options([
+                'dsn' => 'http://public@example.com/sentry/1',
                 'default_integrations' => true,
                 'integrations' => static function (array $defaultIntegrations): array {
                     return $defaultIntegrations;
@@ -230,6 +235,28 @@ public function setupOnce(): void
                 EnvironmentIntegration::class => new EnvironmentIntegration(),
             ],
         ];
+
+        yield 'Default integrations with DSN set to null' => [
+            new Options([
+                'dsn' => null,
+                'default_integrations' => true,
+                'integrations' => static function (array $defaultIntegrations): array {
+                    return $defaultIntegrations;
+                },
+            ]),
+            [
+                'The "Sentry\\Integration\\RequestIntegration" integration has been installed.',
+                'The "Sentry\\Integration\\TransactionIntegration" integration has been installed.',
+                'The "Sentry\\Integration\\FrameContextifierIntegration" integration has been installed.',
+                'The "Sentry\\Integration\\EnvironmentIntegration" integration has been installed.',
+            ],
+            [
+                RequestIntegration::class => new RequestIntegration(),
+                TransactionIntegration::class => new TransactionIntegration(),
+                FrameContextifierIntegration::class => new FrameContextifierIntegration(),
+                EnvironmentIntegration::class => new EnvironmentIntegration(),
+            ],
+        ];
     }
 
     /**
diff --git a/tests/phpt/error_handler_captures_errors_not_silencable_on_php_8_and_up.phpt b/tests/phpt/error_handler_captures_errors_not_silencable_on_php_8_and_up.phpt
index f26fba5..facb159 100644
--- a/tests/phpt/error_handler_captures_errors_not_silencable_on_php_8_and_up.phpt
+++ b/tests/phpt/error_handler_captures_errors_not_silencable_on_php_8_and_up.phpt
@@ -55,7 +55,13 @@ $transportFactory = new class implements TransportFactoryInterface {
 
 error_reporting(E_ALL & ~E_USER_ERROR);
 
-$client = ClientBuilder::create(['error_types' => E_ALL, 'capture_silenced_errors' => false])
+$options = [
+    'dsn' => 'http://public@example.com/sentry/1',
+    'error_types' => E_ALL,
+    'capture_silenced_errors' => false,
+];
+
+$client = ClientBuilder::create($options)
     ->setTransportFactory($transportFactory)
     ->getClient();
 
diff --git a/tests/phpt/error_handler_captures_fatal_error.phpt b/tests/phpt/error_handler_captures_fatal_error.phpt
index 1e970c3..2f684a1 100644
--- a/tests/phpt/error_handler_captures_fatal_error.phpt
+++ b/tests/phpt/error_handler_captures_fatal_error.phpt
@@ -46,7 +46,11 @@ $transportFactory = new class implements TransportFactoryInterface {
     }
 };
 
-$client = ClientBuilder::create([])
+$options = [
+    'dsn' => 'http://public@example.com/sentry/1',
+];
+
+$client = ClientBuilder::create($options)
     ->setTransportFactory($transportFactory)
     ->getClient();
 
diff --git a/tests/phpt/error_handler_respects_capture_silenced_errors_option.phpt b/tests/phpt/error_handler_respects_capture_silenced_errors_option.phpt
index b03612b..ac2c4e2 100644
--- a/tests/phpt/error_handler_respects_capture_silenced_errors_option.phpt
+++ b/tests/phpt/error_handler_respects_capture_silenced_errors_option.phpt
@@ -47,7 +47,12 @@ $transportFactory = new class implements TransportFactoryInterface {
     }
 };
 
-$client = ClientBuilder::create(['capture_silenced_errors' => true])
+$options = [
+    'dsn' => 'http://public@example.com/sentry/1',
+    'capture_silenced_errors' => true
+];
+
+$client = ClientBuilder::create($options)
     ->setTransportFactory($transportFactory)
     ->getClient();
 
diff --git a/tests/phpt/error_handler_respects_current_error_reporting_level.phpt b/tests/phpt/error_handler_respects_current_error_reporting_level.phpt
index 44db315..6b4d8e9 100644
--- a/tests/phpt/error_handler_respects_current_error_reporting_level.phpt
+++ b/tests/phpt/error_handler_respects_current_error_reporting_level.phpt
@@ -10,8 +10,16 @@ declare(strict_types=1);
 
 namespace Sentry\Tests;
 
+use GuzzleHttp\Promise\FulfilledPromise;
+use GuzzleHttp\Promise\PromiseInterface;
 use Sentry\ClientBuilder;
+use Sentry\Event;
+use Sentry\Options;
+use Sentry\Response;
+use Sentry\ResponseStatus;
 use Sentry\SentrySdk;
+use Sentry\Transport\TransportFactoryInterface;
+use Sentry\Transport\TransportInterface;
 
 $vendor = __DIR__;
 
@@ -21,14 +29,36 @@ while (!file_exists($vendor . '/vendor')) {
 
 require $vendor . '/vendor/autoload.php';
 
-$client = ClientBuilder::create([
+$transportFactory = new class implements TransportFactoryInterface {
+    public function create(Options $options): TransportInterface
+    {
+        return new class implements TransportInterface {
+            public function send(Event $event): PromiseInterface
+            {
+                return new FulfilledPromise(new Response(ResponseStatus::success()));
+            }
+
+            public function close(?int $timeout = null): PromiseInterface
+            {
+                return new FulfilledPromise(true);
+            }
+        };
+    }
+};
+
+$options = [
+    'dsn' => 'http://public@example.com/sentry/1',
     'error_types' => null,
     'before_send' => static function () {
         echo 'Before send callback called' . PHP_EOL;
 
         return null;
     },
-])->getClient();
+];
+
+$client = ClientBuilder::create($options)
+    ->setTransportFactory($transportFactory)
+    ->getClient();
 
 SentrySdk::getCurrentHub()->bindClient($client);
 
diff --git a/tests/phpt/error_handler_respects_error_types_option_regardless_of_error_reporting.phpt b/tests/phpt/error_handler_respects_error_types_option_regardless_of_error_reporting.phpt
index bd73e12..e47750b 100644
--- a/tests/phpt/error_handler_respects_error_types_option_regardless_of_error_reporting.phpt
+++ b/tests/phpt/error_handler_respects_error_types_option_regardless_of_error_reporting.phpt
@@ -47,7 +47,12 @@ $transportFactory = new class implements TransportFactoryInterface {
 
 error_reporting(E_ALL & ~E_USER_NOTICE & ~E_USER_WARNING & ~E_USER_ERROR);
 
-$client = ClientBuilder::create(['error_types' => E_ALL & ~E_USER_WARNING])
+$options = [
+    'dsn' => 'http://public@example.com/sentry/1',
+    'error_types' => E_ALL & ~E_USER_WARNING,
+];
+
+$client = ClientBuilder::create($options)
     ->setTransportFactory($transportFactory)
     ->getClient();