/    Sign up×
Community /Pin to ProfileBookmark

Returning error messages from classes

Hi,

I’m writing a set of classes and am trying to find the best way in general to handle errors which may be encountered within these classes.

For instance, i have a class which handles most things to do with a user. One of the class’s methods is editUser which, when called by client code, first evaluates which of the user’s details have been changed. If the email has been changed another method within the same class is called to check that the email does not already exist.

Now, if the email address already exists then i want a suitable error message to be returned to the client code. If no details have been changed then i want another message returned to the client code. If the method completes successfully then i want it to return TRUE (or similar) to the client code. And if it fails for any other reason then i want to return FALSE (or similar).

What is the best regarded solution to doing things like this? One thing i thought of doing was returning codes from each part of the method like:

101 = email exists
102 = no changes made
103 = operation successful
104 = operation failed

..and then the client code can use a switch statement on the object and perform the relevant action.

What works best?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 29.2009 — In most cases I would probably set a class variable with the error information and have the method return false. If you foresee a need for the client code using a switch(), you might have two class variables: error number and error text.
[code=php]
class Foo
{
public $errNo; // last error number
public $errMsg; // last error message

public function bar($x)
{
if($x == 0)
{
$this->errNo = 1;
$this->errMsg = "X is 0";
return false;
}
return true;
}
}
[/code]
Copy linkTweet thisAlerts:
@pavsidauthorOct 29.2009 — In most cases I would probably set a class variable with the error information and have the method return false. If you foresee a need for the client code using a switch(), you might have two class variables: error number and error text.
[code=php]
class Foo
{
public $errNo; // last error number
public $errMsg; // last error message

public function bar($x)
{
if($x == 0)
{
$this->errNo = 1;
$this->errMsg = "X is 0";
return false;
}
return true;
}
}
[/code]
[/QUOTE]


Hiya NogDog,

Going with your first suggestion - setting a class variable and returning false - presumably the client would then test for true/false and if false get the error message? like...

[code=php]echo ($fooh->bar($x))?"Success!":$fooh->getErrMsg();[/code]

The idea for using a switch was just because i hadn't thought the above, but i can see the above much more logical/useful.
Copy linkTweet thisAlerts:
@savvykmsOct 30.2009 — I don't know PHP classes as of yet, but I know that in other various langauges you can define your own classes or structures to create an error handling class.

For example (not PHP code):

class ErrorClass {

var Message; //error message, if applicable

var Error; //error number

};

Simply return an ErrorClass class instance with an Error of 0 and a Message of "No error" or "" if there is no error, otherwise fill it all in.
Copy linkTweet thisAlerts:
@NogDogOct 30.2009 — Hiya NogDog,

Going with your first suggestion - setting a class variable and returning false - presumably the client would then test for true/false and if false get the error message? like...

[code=php]echo ($fooh->bar($x))?"Success!":$fooh->getErrMsg();[/code]

The idea for using a switch was just because i hadn't thought the above, but i can see the above much more logical/useful.[/QUOTE]


Yes, I would have the function return false, then leave it to the client code to decide if it cares about what the specific error was and get the info from the error variable.
Copy linkTweet thisAlerts:
@pavsidauthorOct 30.2009 — Many thanks once again, all the best.
×

Success!

Help @pavsid spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...