Skip to content

Instantly share code, notes, and snippets.

@jippi
Created Feb 20, 2017
Embed
What would you like to do?
<?php
namespace Bownty\Foundation\Error;
use Bownty\Foundation\Environment;
use Cake\Console\ConsoleErrorHandler;
use Exception;
use NewRelic\Lib\NewRelic;
/**
* Error Handler for Cake console. Does simple printing of the
* exception that occurred and the stack trace of the error.
*/
class NewrelicConsoleErrorHandler extends ConsoleErrorHandler {
/**
* Handle uncaught exceptions.
*
* Uses a template method provided by subclasses to display errors in an
* environment appropriate way.
*
* @param \Exception $exception Exception instance.
* @return void
* @throws \Exception When renderer class not found
* @see http://php.net/manual/en/function.set-exception-handler.php
*/
public function handleException(Exception $exception) {
$this->_setupNewRelic();
NewRelic::sendException($exception);
parent::handleException($exception);
}
/**
* Display/Log a fatal error.
*
* @param int $code Code of error
* @param string $description Error description
* @param string $file File on which error occurred
* @param int $line Line that triggered the error
* @return bool
*/
public function handleFatalError($code, $description, $file, $line) {
$this->_setupNewRelic();
NewRelic::sendError($code, $description, $file, $line);
parent::handleFatalError($code, $description, $file, $line);
}
/**
* Set as the default error handler by CakePHP.
*
* Use config/error.php to customize or replace this error handler.
* This function will use Debugger to display errors when debug > 0. And
* will log errors to Log, when debug == 0.
*
* You can use the 'errorLevel' option to set what type of errors will be handled.
* Stack traces for errors can be enabled with the 'trace' option.
*
* @param int $code Code of error
* @param string $description Error description
* @param string|null $file File on which error occurred
* @param int|null $line Line that triggered the error
* @param array|null $context Context
* @return bool True if error was handled
*/
public function handleError($code, $description, $file = null, $line = null, $context = null) {
$this->_setupNewRelic();
NewRelic::sendError($code, $description, $file, $line, $context);
parent::handleError($code, $description, $file, $line, $context);
}
/**
* Setup NewRelic environment
*
* @return void
*/
protected function _setupNewRelic() {
NewRelic::parameter('APP_VERSION', Environment::getAppVersion());
NewRelic::parameter('APP_ENV', Environment::get());
NewRelic::collect();
}
}
<?php
namespace Bownty\Foundation\Error;
use Bownty\Foundation\Environment;
use Cake\Error\ErrorHandler;
use Exception;
use NewRelic\Lib\NewRelic;
/**
* Error Handler provides basic error and exception handling for your application. It captures and
* handles all unhandled exceptions and errors. Displays helpful framework errors when debug > 1.
*
*/
class NewrelicErrorHandler extends ErrorHandler {
/**
* Handle uncaught exceptions.
*
* Uses a template method provided by subclasses to display errors in an
* environment appropriate way.
*
* @param \Exception $exception Exception instance.
* @return void
* @throws \Exception When renderer class not found
* @see http://php.net/manual/en/function.set-exception-handler.php
*/
public function handleException(Exception $exception) {
$this->_setupNewRelic();
NewRelic::sendException($exception);
parent::handleException($exception);
}
/**
* Display/Log a fatal error.
*
* @param int $code Code of error
* @param string $description Error description
* @param string $file File on which error occurred
* @param int $line Line that triggered the error
* @return bool
*/
public function handleFatalError($code, $description, $file, $line) {
$this->_setupNewRelic();
NewRelic::sendError($code, $description, $file, $line);
parent::handleFatalError($code, $description, $file, $line);
}
/**
* Set as the default error handler by CakePHP.
*
* Use config/error.php to customize or replace this error handler.
* This function will use Debugger to display errors when debug > 0. And
* will log errors to Log, when debug == 0.
*
* You can use the 'errorLevel' option to set what type of errors will be handled.
* Stack traces for errors can be enabled with the 'trace' option.
*
* @param int $code Code of error
* @param string $description Error description
* @param string|null $file File on which error occurred
* @param int|null $line Line that triggered the error
* @param array|null $context Context
* @return bool True if error was handled
*/
public function handleError($code, $description, $file = null, $line = null, $context = null) {
$this->_setupNewRelic();
NewRelic::sendError($code, $description, $file, $line, $context);
parent::handleError($code, $description, $file, $line, $context);
}
/**
* Setup NewRelic environment
*
* @return void
*/
protected function _setupNewRelic() {
NewRelic::parameter('APP_VERSION', Environment::getAppVersion());
NewRelic::parameter('APP_ENV', Environment::get());
NewRelic::collect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment