开发者

PHP Handling Business Logic Errors. Maybe a Design-pattern?

开发者 https://www.devze.com 2023-02-14 21:44 出处:网络
Any tips on how to handle business logic errors? I do not mean Exceptions. For example, lest assume that i have a class:

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 cl开发者_Python百科ose to add ErroMessage and ErrorCode attributes to the base business class and check it every time i call a business class method.


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
}


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.


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
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消