Welcome Guest, Not a member yet? Register   Sign In
Email notifications to developer with errors (php errors)
#1

[eluser]seba22[/eluser]
Hello,

First i would like to tell You something, few days back i had problem with my application with many ajax functions / requests...

It can work for all day without any errors, but if some conditions was made.... baam... we have php error what break json response, and this lead to death of ajax site Wink

I spend few days trying to find what's wrong.... i was unable to reproduce on my browser with firebug... because of that conditions... i found error by myself by checking php code line by line ;-)

When error is at main page, user can just contact tech support, send email with output etc, but when it's inside /soemthing/api.php?action=xyz?vlaue=asd hes unable to figure out what's wrong...

Basically that was the moment where i start asking myself "is there any class what will send email with php errors to specified e-mail address" before throwing them to user ?


I see, that Codeigniter errors, are not PHP default, there are somehow nice formatted... thats led me to a question:


Is there something suitable build inside Codeigniter what will email me an errors, before showing them to user ?

Regards
#2

[eluser]InsiteFX[/eluser]
You could extend the Exceptions Class.

See:
Code:
system/core/Exceptions.php

Simple Way To Add Global Exception Handling In CodeIgniter
#3

[eluser]seba22[/eluser]
Hello,

Thank You for answering my question.
I follow tutorial above, but i end with
Code:
Fatal error: Class 'CI_Exceptions' not found in...

I do some readings on my own, and i found answer that i should create

"MY_Exceptions.php" inside "application/libraries/MY_Exceptions.php"

I have done this:

I put code from tutorial
Code:
<?
class MY_Exceptions extends CI_Exceptions {

    public function __construct()
    {

        parent::CI_Exceptions();
    }

    public function show_php_error($severity, $message, $filepath, $line)
    {
$severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
$filepath = str_replace("\\", "/", $filepath);

// For safety reasons we do not show the full file path
if (FALSE !== strpos($filepath, '/'))
{
            $x = explode('/', $filepath);
     $filepath = $x[count($x)-2].'/'.end($x);
}

if (ob_get_level() > $this->ob_level + 1)
{
     ob_end_flush();
}
ob_start();
include(APPPATH.'errors/error_php'.EXT);
$buffer = ob_get_contents();
ob_end_clean();

$msg = 'Severity: '.$severity.'  --> '.$message. ' '.$filepath.' '.$line;

        log_message('error', $msg , TRUE);

mail('[email protected]', 'An Error Occurred', $msg, 'From: [email protected]');

    }

}
?>

Of course i change my e-mail.

Then i create "fake" error message by typing

Code:
print($xyzzaas['asd']);

Inside my controller

I run my code, and get "error"
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: xyzzaas

Filename: controllers/admin.php

Line Number: 21

So i check my e-mail... and got nothing.

Next i edit up

Code:
class MY_Exceptions extends CI_Exceptions {

    public function __construct()
    {
mail('admin@xyz', 'Im here!','aaa');

        parent::CI_Exceptions();
    }

But this email also did not come...

I think that file wont called.

I'm running Codeigniter 2.1.3

If anyone know what i done wrong, please tell me ;-)
#4

[eluser]seba22[/eluser]
Hello,

I found answer why it's not being called.


You have put "MY_Exceptions.php" to "/application/core"

And following working code:
Code:
<?
class MY_Exceptions extends CI_Exceptions {

    public function __construct()
    {
        parent::__construct();
    }

    public function show_php_error($severity, $message, $filepath, $line)
    {
$severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
$filepath = str_replace("\\", "/", $filepath);

// For safety reasons we do not show the full file path
if (FALSE !== strpos($filepath, '/'))
{
            $x = explode('/', $filepath);
     $filepath = $x[count($x)-2].'/'.end($x);
}

if (ob_get_level() > $this->ob_level + 1)
{
     ob_end_flush();
}
ob_start();
include(APPPATH.'errors/error_php'.EXT);
$buffer = ob_get_contents();
ob_end_clean();

$msg = 'Severity: '.$severity.'  --> '.$message. ' '.$filepath.' '.$line;

        log_message('error', $msg , TRUE);

mail('admin@xxxx', 'An Error Occurred', $msg, 'From: [email protected]');
echo $buffer;
    }

}
?>

I have change
Code:
parent::CI_Exceptions();
to
Code:
parent::__construct();


Above full code is working.


What about
Code:
class ExceptionHook
{
  public function SetExceptionHandler()
  {
    set_exception_handler(array($this, 'HandleExceptions'));
  }
  
  public function HandleExceptions($exception)
  {

   $msg ='Exception of type \''.get_class($exception).'\' occurred with Message: '.$exception->getMessage().' in File '.$exception->getFile().' at Line '.$exception->getLine();

        $msg .="\r\n Backtrace \r\n";
$msg .=$exception->getTraceAsString();

        log_message('error', $msg, TRUE);

          
mail('admin@xxx', 'An Exception Occurred', $msg, 'From: [email protected]');


}
}

How test this errors ?

Regards




Theme © iAndrew 2016 - Forum software by © MyBB