Hi, currently I have this client code in my PHP MVC web app:
try {
BookMapper::insert($book);
} catch (DbUniqueConstraintViolationException $e) {
$errorList->addMessage($book . " already exists!");
}
I'm wondering is it bad practice to refer to the low-level framework Db* exceptions to my client code? If so, should I adjust my model code like so:
class BookAlreadyExistsException extends Exception { }
class BookMapper {
public static function insert($book) {
try {
// call to DB-layer to insert $book
// (not relevant to the question)
} catch (DbUniqueConstraintViolationException $e) {
throw new BookAlreadyExistsException();
}
}
}
and then use this new client-code...
try {
BookMapper::insert($book);
} catch (BookAlreadyExistsException $e) {
$errorList->addMessage($book . " already exists!");
}
Or something else? Or is the original method fine?
Thanks!
EDIT: Just want to add, the latter method reads the best IMO, but it comes with the object creation / rethrowing overhead and more significantly, it requires duplicating the rethrowing code in every mapper's insert() method. The former method is easy to implement and catch and works for any model but I remember reading somewhere that you shouldn't do it this way?