0

In my functions.php I have a set of functions used in my website's page templates. I am wondering what is the best way of handling errors. At the moment, my functions.php looks like this:

// include database
require_once 'system/db.php';

// Functions
// =========

function getUrlsPhotos($db, $resultId) {

    $query = "SELECT * FROM pictures WHERE result_id=$resultID ORDER BY id ASC";
    $results = $db -> query($query);

    // Was there an error?
    if ($db -> error) {
        $output = 'error_getting_photos';
    } else {

        // loop for getting photos... ... ...

        $output = $array_photos;

    }

    return $output;

}

function getActivityName($db, $activitiesId) {

    $query = "SELECT * FROM activities WHERE id=$activitiesId LIMIT 1";
    $results = $db -> query($query);
    if ($db -> error) {
        $output = 'db_error_getting_activityname';
    } else {
        $result = $results -> fetch_assoc();
        $output = $result;
    }

    return $output;

}

My goal is to somehow see if an error has occured WITHOUT of course the user seeing it. What is an effective way of handling errors for all my functions? What do I need to initialize?

--- EDIT: ---

Reading more about error handling, I came up with the following script for storing errors in my database:

    <?php
function errorHandler($errno, $errstr, $errfile, $errline) {
    static $db;
    if (empty($db)) {
        $db = new PDO(DSN, DBUSER, DBPASS);
    }

    $query = "INSERT INTO errorlog (severity, message, filename, lineno, time) VALUES (?, ?, ?, ?, NOW())";
    $stmt = $db->prepare($query);

    switch ($errno) {
        case E_NOTICE:
        case E_USER_NOTICE:
        case E_DEPRECATED:
        case E_USER_DEPRECATED:
        case E_STRICT:
            $stmt->execute(array("NOTICE", $errstr, $errfile, $errline));
            break;

        case E_WARNING:
        case E_USER_WARNING:
            $stmt->execute(array("WARNING", $errstr, $errfile, $errline));
            break;

        case E_ERROR:
        case E_USER_ERROR:
            $stmt->execute(array("FATAL", $errstr, $errfile, $errline));
            exit("FATAL error $errstr at $errfile:$errline");

        default:
            exit("Unknown error at $errfile:$errline");
    }
}

set_error_handler("errorHandler");

My questions:

  • What is the best place to put this error handling function in order to be valid for all my php functions? Is it sufficient to put it at the beginning of my functions.php?
  • A disadvantage of this solution surely is that there is no way to get notified if the database is down. How can I achieve that in this case I for example get a notification e-mail?

2 Answers2

1

Use set_error_handler and create a (ideally, but not a necessity) class to handle errors.

set_error_handler(array("ErrorHandling", "doHandleErrors"), E_USER_ERROR | E_NOTICE | E_USER_NOTICE | E_ERROR);

Note: This won't catch FATALs. To handle FATALs, I would use register_shutdown_function().

Your class would be something like this;

<?php

class ErrorHandling  {

    //Methods need to be static

    public static function doHandleErrors($strErrorCode, $strErrorMsg, $strErrorFile, $intErrorLine) {

        //Handle errors.
        //Depending on the $strErrorCode, you would either display the message, or display a http status code 500 & direct to a 500 error page.
        //Depending on the severity of the error, e-mail your programmers.

        //Try not to display any errors to the consumer/user
        //Make the error something like "Ooops, something went wrong." and not "An error occured on file.php, line 23."
        //But that may be more a question for UX.se
    }
}

What is the best place to put this error handling function in order to be valid for all my php functions? Is it sufficient to put it at the beginning of my functions.php?

I would go for putting set_error_handler(array("ErrorHandling", "doHandleErrors"), E_USER_ERROR | E_NOTICE | E_USER_NOTICE | E_ERROR); in your main "brain file" - before your database connection is established, so we can use your custom error handler to handle database connection issues.

A disadvantage of this solution surely is that there is no way to get notified if the database is down. How can I achieve that in this case I for example get a notification e-mail?

With a custom error handler, you can catch the error/exception and e-mail your programmers, then kill the process gracefully, displaying 500 Internal Server Error or 503 Service Unavailable page to the end user.

ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0

You can use set_error_handler in php for custom error handling

check for more

http://php.net/manual/en/function.set-error-handler.php

http://www.sitepoint.com/error-handling-in-php/

PHP : Custom error handler - handling parse & fatal errors

Community
  • 1
  • 1
developerCK
  • 4,418
  • 3
  • 16
  • 35
  • thank you, i followed your links and came up with a script. please see the edit of my post and my two questions at the end. maybe you have an idea? – Claudius Rehme Sep 02 '14 at 08:26