Skip to content

Instantly share code, notes, and snippets.

@dhotson
Created September 8, 2014 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhotson/8770083050dbab10b535 to your computer and use it in GitHub Desktop.
Save dhotson/8770083050dbab10b535 to your computer and use it in GitHub Desktop.
<?php
namespace Bugsnag;
class ErrorHandler
{
private
$client,
$oldExceptionHandler,
$oldErrorHandler,
$reservedMemory;
public function __construct($client)
{
$this->client = $client;
}
public function enable()
{
$this->oldExceptionHandler = set_exception_handler(array($this, 'handleException'));
$this->oldErrorHandler = set_error_handler(array($this, 'handleError'), error_reporting());
register_shutdown_function(array($this, 'handleFatalError'));
$this->reservedMemory = str_repeat('x', 1024 * 10);
}
public function handleException($e)
{
$this->client->exceptionHandler($e);
if ($this->oldExceptionHandler) {
call_user_func($this->oldExceptionHandler, $e);
}
}
public function handleError($code, $message, $file = '', $line = 0, $context=array())
{
$this->client->errorHandler($code, $message, $file='', $line=0);
if ($this->oldErrorHandler) {
call_user_func($this->oldErrorHandler, $code, $message, $file, $line, $context);
}
}
// Stolen from Sentry:
// https://github.com/getsentry/raven-php/blob/8534b131f296597c163cbc408ac2d914f43a1a2c/lib/Raven/ErrorHandler.php
public function handleFatalError()
{
if (null === $lastError = error_get_last()) {
return;
}
unset($this->reservedMemory);
$errors = E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_STRICT;
if ($lastError['type'] & $errors) {
$e = new ErrorException(
@$lastError['message'], @$lastError['type'], @$lastError['type'],
@$lastError['file'], @$lastError['line']
);
$this->handleException($e, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment