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.
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: