doubledee Posted June 8, 2012 Share Posted June 8, 2012 What should I do when a Function errors out? The approach I have taken thus far in my website, is that when an Error occurs, I re-direct the User to an Error Page with the appropriate message. Here is a good example of what I am talking about... function getNewFriendRequestCount($dbc, $requestee){ /** * Return Count of New Friend-Requests * * Takes logged in Member's ID and returns a count of new Friend-Requests * that are marked "Decide later". * * @param Object $dbc * @param Integer $requestee * @return Integer */ // Build query. $q1 = "SELECT COUNT(requestor) FROM friend WHERE requestee=? AND requestor_approved=1 AND requestee_approved=0"; // Prepare statement. $stmt1 = mysqli_prepare($dbc, $q1); // Bind variable to query. mysqli_stmt_bind_param($stmt1, 'i', $requestee); // Execute query. mysqli_stmt_execute($stmt1); // Store results. mysqli_stmt_store_result($stmt1); // Check # of Records Returned. if (mysqli_stmt_num_rows($stmt1)==1){ // Record Found. // Bind result-set to variables. mysqli_stmt_bind_result($stmt1, $newFriendRequestsCount); // Fetch record. mysqli_stmt_fetch($stmt1); }else{ // Record Not Found. /* $_SESSION['resultsCode'] = 'FUNCTION_MEMBER_ID_NOT_FOUND_5001'; // Set Error Source. $_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME']; // Redirect to Outcome Page. header("Location: " . BASE_URL . "/account/results.php"); // End script. exit(); */ } return $newFriendRequestsCount; }//End of getNewFriendRequestCount You can see where I commented out how I normally handle the Errors. And my reasoning was this... Not being able to get "Friend Request Count" is not enough reason to bring my entire page to a crashing halt. (You, of course, could argue the other way too?!) Any thoughts on this? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263842-what-to-do-when-functions-error-out/ Share on other sites More sharing options...
scootstah Posted June 8, 2012 Share Posted June 8, 2012 I don't think the function should be responsible for terminating the application. I think the function should simply return something and that's it. How you handle what is returned should be done in the application itself. Quote Link to comment https://forums.phpfreaks.com/topic/263842-what-to-do-when-functions-error-out/#findComment-1352053 Share on other sites More sharing options...
doubledee Posted June 8, 2012 Author Share Posted June 8, 2012 I don't think the function should be responsible for terminating the application. I think the function should simply return something and that's it. How you handle what is returned should be done in the application itself. So you agree with how I commented out my traditional Error-Handling? (I guess I just need to stick in some sort of Return value in the ELSE where the Error-Handling Code used to be?!) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263842-what-to-do-when-functions-error-out/#findComment-1352057 Share on other sites More sharing options...
scootstah Posted June 8, 2012 Share Posted June 8, 2012 It really depends how the function is used and what depends on it. I don't think the error handling should be done in the function though. What if you wanted to use the function for something else but wanted to handle the failure differently? For example in one part of your script it may be required to return a value but somewhere else it may not be a big deal. Quote Link to comment https://forums.phpfreaks.com/topic/263842-what-to-do-when-functions-error-out/#findComment-1352059 Share on other sites More sharing options...
doubledee Posted June 8, 2012 Author Share Posted June 8, 2012 It really depends how the function is used and what depends on it. I don't think the error handling should be done in the function though. What if you wanted to use the function for something else but wanted to handle the failure differently? For example in one part of your script it may be required to return a value but somewhere else it may not be a big deal. Okay, so I won't make it a practice of going to an Error-Page when my Function dies, unless it it like a mission-critical Function that needs its own dedicated Error-handling. Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263842-what-to-do-when-functions-error-out/#findComment-1352062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.