PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] Yii2 in Third Party systems for ActiveRecord. How to deal with exceptions

 February 16, 2022     activerecord, exception-handling, php, yii, yii2     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing