Welcome Guest, Not a member yet? Register   Sign In
HMVC bug? CI_Exceptions is not working inside models
#1

[eluser]CtheB[/eluser]
Hi All,

I tried to figure this out for hours, but i just can't come to a good solution.

Here is the Case:

- I've overridden the CI_Exceptions class: application/libraries/Exceptions.php
- I'm using HMVC for my modules.
- CI_Exceptions class works great inside:

* application/libraries/
* application/modules/controllers
* application/modules/views

- CI_Exceptions doesn't work inside:

* application/modules/models
* application/models

I'm using PHP v 5.2.6 with codeigniter 1.7.2 and HMVC 5.2.28

Anyone know's why this doesn't work? Is it a HMVC bug or a CI bug?

Thanks in advance. Let me know if you need more info. Let me also know if you have the same or another problem.
#2

[eluser]stuffradio[/eluser]
It would help if you posted more info like code or something, such as how you're trying to use it.
#3

[eluser]CtheB[/eluser]
Oke here is my /application/libraries/Exceptions.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CI_Exceptions {
        // TODO: This function should be mapped to handleException and there be logged to a database / file.
    public function log_exception($severity, $message, $filepath, $line){}

       TODO: map function to handleException and after that show custom 404 page.
    public function show_404($page = '')
    {    
        $heading = "404 Page Not Found";
        $message = "The page you requested was not found.";

        log_message('error', '404 Page Not Found --> '.$page);
        echo $this->show_error($heading, $message, 'error_404', 404);
        exit;
    }
  
        // All general errors will be catched here
    public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
    {
        try
        {
               throw new Exception($heading.' '.$message, $status_code);
           }
           catch(Exception $e)
           {
            self::handleException($e);
        }
    }

    // All normal PHP errors will be catched here
    public function show_php_error($severity, $message, $filepath, $line)
    {    
        try
        {
               throw new Exception($filepath.' '.$message.' on line '.$line, 9);
           }
           catch(Exception $e)
           {
            self::handleException($e);
        }
    }
    
    public static function printException(Exception $e)
    {
           echo 'Caught '.get_class($e).'<br /><strong>'.$e->getFile().'</strong> on line <strong>'.$e->getLine().'</strong> ,<br />code: ' . $e->getCode() . "<br />Message: <strong>".htmlentities($e->getMessage()).'</strong><br />'."\n";
    }
  
    // In your application you can catch your errors with this function CI_Exceptions::handleException($e);
    // TODO: If production enviroment: Log message to database if possible and show 404 page, otherwise to file. If test enviroment: printException.
    public static function handleException(Exception $e)
    {
            self::printException($e);
    }
}
set_exception_handler(array("CI_Exceptions", "handleException"));

Note: Because the Exception class is autoloaded by codeigniter, we don't need to load it ourselfs.
If we try to load the library we get other errors (the main error (bug) i get is, that my javascript & css file in the &lt;head&gt; of my html is showed in the &lt;body&gt; part of the html inside of firebug. (and the design is looking strange in IE)

Here is how to handle it inside controllers / libraries:
Code:
try
{
            // some code wich can fail
}
catch(Exception $e)
{
            CI_Exceptions::handleException($e);
}

I want to use it also inside of my Models like this:

Code:
public function get_keywords($subject)
{
    $query = "  SELECT      word, hits
                  FROM        search
                  WHERE       word LIKE :subject
                  ORDER BY    count DESC
                  LIMIT       0, 10
        ";
        
try
{
            $stmt = $this->db->prepare($query);
            $stmt->bindValue(':subject', "$subject%", PDO::PARAM_STR);
            $stmt->execute();
            $result = $stmt->fetchAll();
}
catch(PDOException $e)
{
            CI_Exceptions::handleException($e);
}
        
         return $result;
}

This last code fails. It throws an Uncaught exception, it is not cached by the handleException method.
If i load the Exception class myself inside the controller or model, this code WILL work, however, i'll get some
problems with the css displaying strange on the page (because the css file is not in the &lt;head&gt; anymore but it
appears to be in the &lt;body&gt; part of the html (if i look in firebug).

So the real problem is that, while my Exception class is instantiated by codeigniter, it will not work inside my models.
#4

[eluser]CtheB[/eluser]
Who does work with PHP Exceptions in Codeigniter?

Is there anyone who could help me with this?
#5

[eluser]CtheB[/eluser]
Well i've got it working. Not very ellegant, but in a codeigniter way.

My code was:
Code:
catch(PDOException $e)
{
            CI_Exceptions::handleException($e);
}

And now it is:
Code:
catch(PDOException $e)
{
             show_error($e);
}

Only the helper is autoloaded and the helper loads the class i think. So now the show_error is loading the CI_Exceptions:Confusedhow_error wich is loading the CI_Exceptions:handleException. So it finally works for me now.




Theme © iAndrew 2016 - Forum software by © MyBB