Created
June 29, 2015 13:41
-
-
Save Blizzke/b6b2fad6dad319f0afd1 to your computer and use it in GitHub Desktop.
Alternative to error catching before bootstrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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