1

Any tips on how to handle business logic errors? I do not mean Exceptions. For example, lest assume that i have a class:

<?php
class Reactor () {  // business class 
    public function shutdown() {  
    if($date > '2 pm') {  
        // show error message to user  
        echo 'you can't shutdown before 2 pm.';  
       } else {  
        // error while trying to shutdown  
           throw new Exception('Oh my God, it is gonna blow!!');  
        }
    }
}
?>

The real question is how to pass the error message to my views? Exceptions are good for exceptional cases. I'm very close to add ErroMessage and ErrorCode attributes to the base business class and check it every time i call a business class method.

Cœur
  • 37,241
  • 25
  • 195
  • 267
uacaman
  • 418
  • 1
  • 5
  • 15

3 Answers3

2

You're actually on the right track here. You can handle the exceptions in your ErrorController - a convention modeled in Zend, but in many other frameworks too. You can create your own if you're rolling it DIY.

This thread has a more Zend-centric method of handling, but you can use the ErrorController to actually render your view. Handle the input of the $e exception class and get the message from that.

Throwing exceptions from model/view/controller in a Zend Framework application

If you're deep in the DIY route, you can display it gracefully if you wrap your larger blocks in try/catch and test all instances of the exception class. For instance:

class Reactor () {  // business class 
    public function shutdown() {  
    if($date > '2 pm') {  
        // show error message to user  
        echo "you can't shutdown before 2 pm.";  
       } else {  
        // error while trying to shutdown  
           throw new Exception('Oh my God, it is gonna blow!!');  
        }
    }
}

//later, in the controller

$reactor = new Reactor();
try{
  $reactor->shutdown('1pm');
} catch(Your_Custom_Exception $e){
  //pass to view
  $this->view($e->getMessage());
} catch(Exception $e){
  // woops, serious error. do something useful
}
dwenaus
  • 3,206
  • 2
  • 27
  • 27
philwinkle
  • 7,036
  • 3
  • 27
  • 46
2

Exceptions are exactly what you need in this case. State validation (this is what you're doing) shall lead either to silence or an exception. You shall handle Model-thrown exceptions in your Controller, convert them to messages and pass them to View.

yegor256
  • 102,010
  • 123
  • 446
  • 597
1

I think you should have something like this.

Use attributes to store data and error message. And i think it is illogical to generate error for if and else too

class Reactor{

    public $date;
    public $error;
    public $errorstatus = false;
    //Use property to store data and errors

    public function shutdown() {  
    if($date > 2) {  

        $this->errorstatus = true;
        $this->error['date'] = "You cannot shutdown before 2 pm";

    } else 
        return true;
    }

}

$reactor = new Reactor();

$reactor->data = 3;

$reactor->shutdown();

if($reactor->errorstatus){
    echo $reactor->error['date'];   
}
else{
    echo "Its already two you can shutdown";
}

echo "<br/>";

$reactor->data = 1;

$reactor->shutdown();

if($reactor->errorstatus){
    echo $reactor->error['date'];   
}
else{
    echo "Its already two you can shutdown";
}

[UPDATE]

    public function shutdown() {  
    if($date > 2) {  

        $this->errorstatus = true;
        $this->error['date'] = "You cannot shutdown before 2 pm";

    } else 

        if($this->poweroff)
            return true;
        else
            throw new Exception("ERROR WHILE SHUTTING DOWN"):
    }

    private function poweroff()
    {
        //if power off then return true
        //else return false
    }
S L
  • 14,262
  • 17
  • 77
  • 116
  • He's not generating an error on the if and else too. He means that the else should actually try to shutdown, but an error might arise in the process. – Sebastián Grignoli Mar 06 '11 at 04:38
  • @Sebastian does he mean if the try to shutdown, he can't then generate error, then i will update code – S L Mar 06 '11 at 04:41
  • Is is my undertending that a bussines error should no generate a Exception http://stackoverflow.com/questions/534096/should-a-business-rule-violation-throw-an-exception, so i´m trying to avoid catching custon sxceptions for bussines logic erro. I think i´m going to go with ErrorCode and ErroMessage. Thank all foe the help. – uacaman Mar 09 '11 at 18:23
  • @uacaman You get error for example:- (10/0) which is infinity, which is an error :- division by zero. In this case you get ugly error like syantax error. In this case you should use try catch to generate Exception. In business logic, you just do permit some actions, for eg.(shutdown before 2), instead you generate logial error message – S L Mar 10 '11 at 04:52