Permalink
Browse files

Added error handlers

  • Loading branch information...
1 parent ac98b32 commit d86318511dbaff214ec7bb23f2a2c7a267d06ebb @dcramer dcramer committed Feb 7, 2012
Showing with 81 additions and 1 deletion.
  1. +5 −0 README.rst
  2. +1 −1 composer.json
  3. +38 −0 lib/Raven/ErrorHandler.php
  4. +37 −0 test/Raven/Tests/ErrorHandlerTest.php
View
@@ -22,6 +22,11 @@ raven-php is an experimental PHP client for `Sentry <http://aboutsentry.com/>`_.
echo "Sorry, there was an error!";
echo "Your reference ID is " . $event_id;
+ // Install error handlers
+ $error_handler = new Raven_ErrorHandler($client);
+ set_error_handler(array($error_handler, 'handleError');
+ set_exception_handler(array($error_handler, 'handleException'));
+
Resources
---------
View
@@ -4,7 +4,7 @@
"description": "A PHP client for Sentry (http://getsentry.com)",
"keywords": ["log", "logging"],
"homepage": "http://getsentry.com",
- "version": "0.1",
+ "version": "0.2.0",
"license": "Apache License 2",
"authors": [
{
@@ -0,0 +1,38 @@
+<?php
+
+/*
+ * This file is part of Raven.
+ *
+ * (c) Sentry Team
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Event handlers for exceptions and errors
+ *
+ * $client = new Raven_Client('http://public:secret/example.com/1');
+ * $error_handler = new Raven_ErrorHandler($client);
+ * set_error_handler(array($error_handler, 'handleError');
+ * set_exception_handler(array($error_handler, 'handleException'));
+ *
+ * @package raven
+ */
+
+class Raven_ErrorHandler
+{
+ function __construct($client) {
+ $this->client = $client;
+ }
+
+ function handleException($e) {
+ $this->client->captureException($e);
+ }
+
+ function handleError($code, $message, $file='', $line=0, $context=array()) {
+ $e = new ErrorException($message, 0, $code, $file, $line);
+ $this->handleException($e);
+ }
+}
+?>
@@ -0,0 +1,37 @@
+<?php
+
+/*
+ * This file is part of Raven.
+ *
+ * (c) Sentry Team
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+class Raven_Tests_ErrorHandlerTest extends PHPUnit_Framework_TestCase
+{
+ public function testErrorsAreLoggedAsExceptions()
+ {
+ $client = $this->getMock('Client', array('captureException'));
+ $client->expects($this->once())
+ ->method('captureException')
+ ->with($this->isInstanceOf('ErrorException'));
+
+ $handler = new Raven_ErrorHandler($client);
+ $handler->handleError(E_WARNING, 'message');
+ }
+
+ public function testExceptionsAreLogged()
+ {
+ $client = $this->getMock('Client', array('captureException'));
+ $client->expects($this->once())
+ ->method('captureException')
+ ->with($this->isInstanceOf('ErrorException'));
+
+ $e = new ErrorException('message', 0, E_WARNING, '', 0);
+
+ $handler = new Raven_ErrorHandler($client);
+ $handler->handleException($e);
+ }
+}

0 comments on commit d863185

Please sign in to comment.