Jump to content

Problem with Error Capture


Muddy_Funster

Recommended Posts

Ok, I'm trying to play with custom error handeling, and not getting anywhere. 

I have some code I made up just to mess about and get a feel for this, but no matter what I try I get nothing thrown out and no error caught. 

I did have it all in the one class, but got strongly advised that was a bad bad bad thing to do, so in an effort to keep the focus on the actual problem I've moved it off into it's own class.

 

Here's the code, it's self contained (as I said its only a play around) but I would really appreciate it if someone could explain why the errors arn't being caught:

<?php
//------------------------------------------------
class dbc {
public $server;
public $host;
public $db;
public $user;
public $pass;

public function set($var, $val=0){
try{
  if(is_array($var)){
   foreach ($var as $k => $v) {
    if(!$this->$k = $v){
     throw new Exception('Error Setting Database Information :');
    }     
   }  	
  }
  else{
   if(!$this->$var = $val){
    throw new Exception('Error Setting Database Information :');
   }
  }
}
catch(Exception $e){
  $setError = array('1', 'set', $e->getMessage());
  $error->ErrorHandle($setError);
}
}
}
//------------------------------------------------    
class errorCatch{
public $severity;
public $methodCall;
public $message;

public function ErrorHandle($error){
$this->severity = $error['0'];
$this->methodCalled = $error['1'];
$this->message = $error['2'];
if ($this->severity == 1){
  die ("Critical error in call to dbo->$methodCall: $message");
}
die("Warning Call to $methodCall resulted in a non fatal error: $message");
}
}
//------------------------------------------------
$error = new errorCatch;
$con = new dbc;
$set = array('hst'=>'localhost', 'svr'=>'mysql');
$con -> set($set);
var_dump($error);
?>

Link to comment
Share on other sites

OK, made some changes based on what you said, code is now :

<?php
//------------------------------------------------
class dbc {
public $server;
public $host;
public $db;
public $user;
public $pass;
public $hasError;

public function set($var, $val=0){
try{
  if(is_array($var)){
   foreach ($var as $k => $v) {
    if(!$this->$k = $v){
     throw new Exception('Error Setting Database Information :');
    }     
   }  	
  }
  else{
   if(!$this->$var = $val){
    throw new Exception('Error Setting Database Information :');
   }
  }
}
catch(Exception $e){
  $setError = array('1', 'set', $e->getMessage());
  $error = new errorCatch;
  $error->ErrorHandle($setError);
  $this->hasError = "caught the error";
}
}
}
//------------------------------------------------    
class errorCatch{
public $severity;
public $methodCall;
public $message;

public function ErrorHandle($error){
$this->severity = $error['0'];
$this->methodCalled = $error['1'];
$this->message = $error['2'];
if ($this->severity == 1){
  die ("Critical error in call to dbo->$methodCall: $message");
}
die("Warning Call to $methodCall resulted in a non fatal error: $message");
}
}
//------------------------------------------------
//$error = new errorCatch;
$con = new dbc;
$set = array('hst'=>'localhost', 'svr'=>'mysql');
$con -> set($set);
var_dump($con);
?>

which interestingly gave this as an output :

object(dbc)#1 ( { ["server"]=> NULL ["host"]=> NULL ["db"]=> NULL ["user"]=> NULL ["pass"]=> NULL ["hasError"]=> NULL ["hst"]=> string(9) "localhost" ["svr"]=> string(5) "mysql" } 

 

It's appending new variables into the class without them being pre defined :( so it's effectivly not generating an error.

I guess need a better check (ie. one that works), any suggestions?

Link to comment
Share on other sites

You don't need to use $this because it's within the scope of the function.

 

Also on your if statements, are you trying to check if the value of $k isn't equal to the value of $v? If so, then you would use $k != $v, because what you're saying now is $v is set to NOT $k which would become false regardless.

Link to comment
Share on other sites

I'm trying to cheack if $v is being assigned to an existing public var defined at the top of the class.  if $k does not match any of the defined variables($host, $server, $user, $pass, $db - the other one is only there just now for testing) throw an error, if it does match assign $v to $this->$k

 

problem is, if it doesn't find an existing variable by the name of $k ir just goes and makes a new one :/

Link to comment
Share on other sites

Yeah, you're definitely trying to run before you can walk.

 

The simplest, most OO thing to do would be to extend/subclass Exception itself.  Right now, you're wrapping it in another class for no real reason.  Even though Exception is provided by PHP itself, it's a normal class.  That will have you thinking in terms of the exception itself rather than the contortions you're trying to put it through.

 

class SQLException extends Exception {
   private $severity;

   public function __construct($message = "", $severity = 0) {
      parent::__construct($message);

      $this -> severity = $severity;
   }

   public function getSeverity() {
      return $this -> severity;
   }
}

 

Just a basic example of how you could make your own Exception.  Throwing/catching is the same as always:

 

throw new SQLException("Something went wrong!");
.
.
.
catch (SQLException $e) {
   // ...
}

Link to comment
Share on other sites

thanks for that Kevin.  I'm still not actualy throwing out anything on the comparison check, it's just making new class attributes if the $this->$k / $this->$var doesn't already exist.  How do I get it to throw an error if the attribute isn't an existing predefined one?  Example - using the code as it is now :

<?php
//------------------------------------------------
class dbc {
public $server;
public $host;
public $db;
public $user;
public $pass;
public $hasError;

public function set($var, $val=0){
$atts = (array) $this;
try{
  if(is_array($var)){
   foreach ($var as $k => $v) {
    if(!$this->$k = $v){
     throw new custError(" '$k' is not a valid parameter");
    }     
   }  	
  }
  else{
   if(!$this->$var = $val){
    throw new custError(" '$var' is not a valid parameter");
   }
  }
}
catch(custError $e){      
  $msg = $e->getEreturn();
  $level = $msg['0'];
  $txt = $msg['1'];
  $eLocation = 'dbo->set()';
  $error = new errorCatch;
  $setError = array($level, $eLocation, $txt);
  $error->ErrorHandle($setError);
  $this->hasError = "caught the error";
}
}
}
//------------------------------------------------ 
class custError extends Exception{
private $eLevel;
private $eMessage;

public function __construct($message = "", $severity = 0){
parent::__construct($message);
$this -> eLevel = $severity;
$this -> eMessage = $message;
}

public function getEreturn(){
$eOut = array($this->eLevel, $this->eMessage);
return $eOut;
}
}
//------------------------------------------------   
class errorCatch{
public $severity;
public $methodCall;
public $message;

public function ErrorHandle($error){
$this->severity = $error['0'];
$this->methodCall = $error['1'];
$this->message = $error['2'];
if ($this->severity == 1){
  die ("Critical error in call to $this->methodCall : $this->message");
}
die("Warning Call to $this->methodCall resulted in a non fatal error : $this->message");
}
}
//------------------------------------------------
//$error = new errorCatch;
$con = new dbc;
$set = array('host'=>'localhost', 'svr'=>'mysql');
$con->set($set);
var_dump($con);
?>

I want it to throw an error saying "srv is not a valid property name" instead it just goes and makes a new attribute called srv in the class and populates it with the value of 'mysql'.  Do I need to cast $this into an array and check the array keys V's the input?  I've tried searching for other ways but cant get anything that helps with this problem.

 

Cheers

Link to comment
Share on other sites

ok, being as I'm the impatient type I went ahead and changed the checks to user the $this array, it's now throwing what I want, but I can't help but feel there has to be a better way to do the comparison.  This is what I've ended up with (thanks in no small part to the help I have had here) :

<?php
//------------------------------------------------
class dbc {
public $server;
public $host;
public $db;
public $user;
public $pass;
public $hasError;

public function set($var, $val=0){
$atts = (array) $this;
try{
  if(is_array($var)){
   if(count(array_diff_key($var, $atts)) >= 1){
    $eek = implode(', ',array_flip(array_diff_key($var, $atts)));
    throw new custError(" '$eek' is/are not (a) valid parameter(s)", 1);
   }
   else{
   foreach ($var as $k => $v) {
    $this->$k = $v;     
    }     
   }  	
  }
  else{
   if (array_key_exists($var, $atts) === false){
    throw new custError(" '$var' is not a valid parameter", 1);
   }
   else{
    $this->$var = $val;
   }
  }
}
catch(custError $e){      
  $msg = $e->getEreturn();
  $level = $msg['0'];
  $txt = $msg['1'];
  $eLocation = 'dbo->set()';
  $error = new errorCatch;
  $setError = array($eLocation, $txt);
  $error->ErrorHandle($setError, $level);
  $this->hasError = "caught the error";
}
}
}
//------------------------------------------------ 
class custError extends Exception{
private $eLevel;
private $eMessage;

public function __construct($message = "", $severity = 0){
parent::__construct($message);
$this -> eLevel = $severity;
$this -> eMessage = $message;
}

public function getEreturn(){
$eOut = array($this->eLevel, $this->eMessage);
return $eOut;
}
}
//------------------------------------------------   
class errorCatch{
public $severity;
public $methodCall;
public $message;

public function ErrorHandle($error,$level){
$this->methodCall = $error['0'];
$this->message = $error['1'];
$this->severity = $level;
if ($this->severity == 1){
  die ("<b>Critical</b> : Error in call to $this->methodCall : $this->message");
}
die("<b>Warning</b> : Call to $this->methodCall resulted in a non fatal error : $this->message");
}
}
//------------------------------------------------
//$error = new errorCatch;
$con = new dbc;
$set = array('host'=>'localhost', 'server'=>'mysql');
//$set = 'server';
$con->set($set, 'mssql');
var_dump($con);
?>

I'm still playing with it, so if anyone can suggest a better comparison for the try I'd be happy to hear it.

Link to comment
Share on other sites

    if(!$this->$k = $v){

 

That does not check if $k exists on $this, it checks if $v is a false value.  It's essentially the same as doing

if (!$v){ 
}

 

To check if the variable given by $k exists on the object, use property_exists or isset.

 

if (!isset($this->$k)){
    throw new custError(" '$k' is not a valid parameter");
}
else {
   $this->$k = $v;
}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.