Jump to content

Error-handling problem


GarrettW

Recommended Posts

Hi, I'm writing a CMS and I'm having trouble making PHP handle errors the way I want it to. The code looks fine to me, but PHP acts like it doesn't see it.

I'm including just the necessary parts of the files in question; see if you can tell me what I'm doing wrong.

 

index.php

include 'core/Error.class.php';
require 'core/DB.class.php';
  $db = DB::singleton();

 

core/DB.class.php

class DB
{
  protected $mydb;
  private static $instance;

  private function __construct () {
    $this->mydb = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_NAME,MYSQL_PORT)
      or Error::mysqli_connect();
    }
  }
  static function singleton () {
    if (!isset(self::$instance)) {
      self::$instance = new self();
    } return self::$instance;
  }
}

 

core/Error.class.php

define('E_FATAL', E_USER_ERROR);
define('E_NONFATAL', E_USER_WARNING);
define('E_SUGGESTION', E_USER_NOTICE);

class Error {
  static $codes = array(400 => 'Bad Request', 401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 500 => 'Internal Server Error');

  static function send ($httpcode,$severity,$text) {
    if (!headers_sent()) header("HTTP/1.0 $httpcode {$codes[$httpcode]}");
    trigger_error($text,$severity);
  }

  static function mysqli_connect () {
    self::send(500,E_FATAL,'Could not connect to database.<br>Error '.mysqli_connect_errno().': '.mysqli_connect_error());
  }
}

 

Generated output:

Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can't connect to MySQL server on 'localhost' (10061) in D:\xampp\htdocs\CwMS\core\DB.class.php on line 18

 

Expected output:

Could not connect to database.

Error 10061: Can't connect to MySQL server on 'localhost'

Link to comment
https://forums.phpfreaks.com/topic/160791-error-handling-problem/
Share on other sites

How dumb - I can't edit my post now. Well, consider this a revision then. I changed the Error class a bit.

 

core/Error.class.php

define('E_FATAL', E_USER_ERROR);
define('E_NONFATAL', E_USER_WARNING);
define('E_SUGGESTION', E_USER_NOTICE);

class Error {
  static $codes = array(400 => 'Bad Request', 401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 500 => 'Internal Server Error');
  static $elevel = array(E_FATAL => 'Fatal error', E_NONFATAL => 'Error', E_SUGGESTION => 'FYI');

  static function send ($httpcode,$severity,$text) {
    if (!headers_sent()) header("HTTP/1.0 $httpcode ".self::$codes[$httpcode]);
    echo '<b>'.self::$elevel[$severity].'</b>: '.$text;
    if ($severity == E_FATAL) die();
  }

  static function mysqli_connect () {
    self::send(500,E_FATAL,'Could not connect to database.<br>Error '.mysqli_connect_errno().': '.mysqli_connect_error());
  }
}

Didn't change the generated output.

 

New expected output:

Fatal error: Could not connect to database.

Error 10061: Can't connect to MySQL server on 'localhost'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.