Skip to content

Instantly share code, notes, and snippets.

@Blizzke
Created June 29, 2015 13:41
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 Blizzke/b6b2fad6dad319f0afd1 to your computer and use it in GitHub Desktop.
Save Blizzke/b6b2fad6dad319f0afd1 to your computer and use it in GitHub Desktop.
Alternative to error catching before bootstrap
<?php
/**
* Error Handler allows errors to be logged to the audit_error table.
*/
namespace bedezign\yii2\audit\components\base;
use bedezign\yii2\audit\Audit;
use bedezign\yii2\audit\models\AuditError;
use Exception;
use Yii;
/**
* ErrorHandlerTrait
* @package bedezign\yii2\audit\components\base
*/
trait ErrorHandlerTrait
{
/**
* @param Exception $exception
*/
public function logException($exception)
{
try {
// Make sure not to interfere with out of memory errors, there's not enough spare room to load everything
if (strncmp($exception->getMessage(), 'Allowed memory size of', 22) == 0) {
return;
}
$module = Audit::getInstance();
if (!$module) {
$module = \Yii::$app->getModule(Audit::findModuleIdentifier());
if (!$module)
throw new \Exception('No module found to log to');
}
$entry = $module->getEntry(true);
if ($entry) {
AuditError::log($entry, $exception);
$entry->finalize();
}
} catch (\Exception $e) {
// if we catch an exception here, let it slide, we don't want recursive errors killing the script
}
parent::logException($exception);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment