What's the most common / best practice for handling possible MySQL errors in an PHP MVC application? Would it be best to pass an success boolean from the model to controller or to throw exceptions? Lets say I'm calling a stored procedure and the possible errors I could encounter are, db connection, the user does not have permission, invalid data, or a random MySQL error, what could be the most efficient / effective method for handling.
For example: Method 1:
//UserController.php
private function get_user_info(){
$user_info = $user_model->read_user_info(123);
if($user_info[0]){
//Do stuff with user data
}else{
//Check if db, permission, invalid data, or random MySQL error
}
}
//UserModel.php
public function read_user_info($read_user_id){
$stmt = $db->prepare("CALL read_user_info(?, ?)");
$stmt->bindParam(1, $current_user_id);
$stmt->bindParam(2, $read_user_id);
if($stmt->execute()){
$result_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
//Does the user have permission to read other user's info
if($result_set["granted"]){
return array(true, $result_set["user_info"]);
}else{
return array(false, "Permission error");
}
}else{
return array(false, "MySQL error");
}
}
Method 2:
//UserController.php
private function get_user_info(){
try{
$user_info = $user_model->read_user_info(123);
//Do stuff with user data
}catch(ConnectionException $e){
}catch(InvalidDataException $e){
}catch(MySQLException $e){
}
}
//UserModel.php
public function read_user_info($read_user_id){
$stmt = $db->prepare("CALL read_user_info(?, ?)");
$stmt->bindParam(1, $current_user_id);
$stmt->bindParam(2, $read_user_id);
if($stmt->execute()){
$result_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
//Does the user have permission to read other user's info
if($result_set["granted"]){
return $result_set["user_info"];
}else{
throw new PermissionException();
}
}else{
throw new MySQLException();
}
}