swiftyper / emperror-php
Provides an error handler for PHP that can execute a stack of handlers for various purposes.
Requires
- php: ^7.2 || ^8.0
- ext-curl: *
- ext-json: *
- jean85/pretty-package-versions: ^1.5 || ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpunit/phpunit: ^8.2
- symfony/var-dumper: ^5.2
README
Emperror PHP
PHP error catcher and handler for swiftyper.sk.
Emperror catches PHP exceptions, errors, warnings, and notices.
๐ Requirements
- PHP 7.2 or higher
- PHP extensions:
curl,json - Composer is required for installation
๐ฆ Installing
$ composer require swiftyper/emperror-php
โก๏ธ Quick Start
Configuration
\Swiftyper\Emperror::setup([ 'apiKey' => 'your api key' ]);
\Swiftyper\Emperror::get() ->setUser([ 'name' => 'user name', 'photo' => 'user photo', ]) ->setContext([ 'orderId' => 1234, ]);
Send events and exceptions manually
Use sendException() to send any caught exception:
try { throw new Exception("Payment processing error", 1); } catch (Exception $e) { \Swiftyper\Emperror::get()->sendException($e); }
Use sendMessage() to send a plain message with optional context - ideal for logs,
notices, or anything that isn't an exception:
\Swiftyper\Emperror::get()->sendMessage('Payment webhook received twice', [ 'webhookId' => $webhookId, ]);
Use sendEvent() when you need full control over the raw event payload:
\Swiftyper\Catcher::get()->sendEvent([ 'title' => 'Custom event title', 'context' => [ 'orderId' => 1234, ], ]);
Filtering sensitive information
Emperror already filter a few obvious things for you before an event ever leaves your server:
- Sensitive request headers -
Authorization,Cookie,Proxy-Authorization,X-Api-Key,X-Auth-Token,X-Csrf-Token- are replaced with[Filtered]. - Function/method arguments whose name looks like a secret (
password,token,secret,apiKey,auth, ...) are replaced with[Filtered]in stack traces.
For anything else - user emails, custom context fields, or your own payloads - use
the beforeSend hook. Use the EventPayload getters/setters to inspect or clear any
property before it's sent:
\Swiftyper\Emperror::setup([ // ... 'beforeSend' => function (\Swiftyper\EventPayload $eventPayload) { $user = $eventPayload->getUser(); if (!empty($user['email'])){ unset($user['email']); $eventPayload->setUser($user); } return $eventPayload; } ]);
๐งช Testing
$ composer tests
๐ค Contributing
Please see CONTRIBUTING for details.
๐ License
This project is licensed under the MIT License. See the LICENSE file for details.