Jump to content

What to do when Functions error-out?


doubledee

Recommended Posts

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.