Issue
I use Yii2 with my framework because I like ActiveRecord and QueryBuilder. Yii2 Official docs describes how to use it.
It works, but Yii2 takes all control of PHP exceptions and warnings in ErrorHandler.php
/**
* Register this error handler
*/
public function register()
{
ini_set('display_errors', false);
set_exception_handler([$this, 'handleException']);
if (defined('HHVM_VERSION')) {
set_error_handler([$this, 'handleHhvmError']);
} else {
set_error_handler([$this, 'handleError']);
}
if ($this->memoryReserveSize > 0) {
$this->_memoryReserve = str_repeat('x', $this->memoryReserveSize);
}
register_shutdown_function([$this, 'handleFatalError']);
}
I don't know how to deal with it.
For example, I have DBException form Yii. If I set my own set_exception_handler
, it will have very poor information about exception: only code and message. It will be very difficult to debug it without prepared query, query parameters, etc.
If I use Yii2's exception handler - I have to rewrite all my framework with Yii2 exceptions. Thats not good and I don't really like Yii2 letters and exception templates. All I need from Yii2 is work with DB.
Do you have any ideas how can I solve this situation?
Solution
I realized, that Yii's Database Exception has all information about request and error. Also that Exception has some additional methods for another info. Thats enough to controll all Exceptions and errors by my Framework as usual.
So I rewrited all handlers again to my handlers
spl_autoload_register(array("MyClass", 'autoload'));
set_exception_handler(['MyClass','exceptionHandler']);
set_error_handler(['MyClass','errorHandler']);
and collect all usefull information to error body
$body .= "Error: " . $e->getMessage() . PHP_EOL;
$body .= "File: " . $e->getFile() . ":" . $e->getLine() . PHP_EOL;
$body .= "Trace:" .$e->getTraceAsString() . PHP_EOL;
$prev = $e->getPrevious();
if ($prev) {
$body .= "Next To: ";
$body .= get_class($prev)." ".PHP_EOL;
$body .= $prev->getMessage();
}
if ($e instanceof yii\db\Exception) {
$body .= "Additional Info: " . (print_r($e->errorInfo, true));
}
Answered By - John Kakon
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.