0

I am having trouble coming up with a solution to handle success or failure(errors) that is consistent throughout my whole application, so far I have been stuck deciding from 3 possible solutions.(I am relatively new to OOP)

For example, if I wanted to login a remote api using CURL, I could make a class with methods structured similar to the following.

class Api
{
    var $curl;
    var $user;
    var $pass;
    var $buffer;

    function __construct($user,$pass)
    {
        $this->curl = new curl();
        $this->user = $user;
        $this->pass = $pass;
    }

    functon login()
    {
        $url = 'http://example.com/login/';
        $postfields = array($user => $this->user, $pass => $this-pass);

        $this->buffer = $this->curl->post($url,$postfields);
    }

}

After using login() we have some possible outcomes, successful login, bad login(with info from api such as user/pass incorrect), failed pageload(server unavailable) or possibly CURL errors.(many more issues possible but listed enough)

One basic approach would be create another method loginSuccessful()

loginSuccessful()
{
    if(strstr($this->buffer,'Welcome user: "zzzz"'))
        return true;

    if(strstr($this->buffer,'Incorrect user or pass'))
        return false;

    if(strstr($this->buffer,'server unavailable'))
        return false;

    if(curl_error($this->curl->ch))
        return false;

    else
        return false;
}

I could call this within the login() method after we post the data.

    functon login()
    {
        $url = 'http://example.com/login/';
        $postfields = array($user => $this->user, $pass => $this-pass);

        $this->curl->post($url,$postfields);

        if(loginSuccessul())
            return true;
        else
            false;
    }

This way is of course too basic and doesn't give much information about the error, to improve this I could return true still if successful or a error message if unsuccessful(instead of false).

loginSuccessful()
{
    if(strstr($this->buffer,'Welcome user: "zzzz"'))
        return true;

    if(strstr($this->buffer,'Incorrect user or pass'))
        return 'Incorrect user or pass';

    if(strstr($this->buffer,'server unavailable'))
        return 'server down';

    if(curl_error($this->curl->ch))
        return curl_error($this->curl->ch);

    else
        return 'some other error';
}

We use this the same as before, however something doesn't feel right about returning different types.(sometimes boolean, sometimes strings)

Another possible way would be to keep the true/false style as in the first method, but build another method like loginError(), which we place with login() again like

    functon login()
    {
        ...
        ....
        if(loginSuccessul())
            return true;
        else
            return loginError();
    }

This now gives us some detailed error messages, but even this method doesn't feel right because overall I am mixing alot of text information with logic, also seems like I am breaking some SOLID principles.

Possibly the problem is with the class structure and my approach, all what I am doing is incorrect because I am working within a bad structure/mindset.

Thanks in advance for any thoughts.

cecilli0n
  • 457
  • 2
  • 9
  • 20

1 Answers1

0

In order to get this right: You want a login function which does return something so you know that everything went fine plus something additional to determine what did not work if the function fails?

I'm aware of 2 possibilities to archieve this:

  1. return bool (true/false wether it worked or not) and provide an additional Error String in your class which doesn't get returned and can be set individually. See GetLastError from MSDN for further information. You could implement something like that in your PHP script.

  2. Exceptions ( -> PHP: Exception - Manual )

    if(strstr($this->buffer,'server unavailable')) throw new Exception("some wild     exception occured, feed me with some data and catch me later");
    

Personally I'd recommend you to use exceptions since they are usually better/easier to maintain and less time consuming than manually structuring errorcodes and errorstrings.

0x8BADF00D
  • 962
  • 1
  • 8
  • 18
  • correct, I assumed the example I provided would be a general problem that arises in many applications, so I also thought it would have a well established approach to handling. I am going to research exceptions now. Thanks – cecilli0n Mar 23 '14 at 22:45
  • After doing research on exceptions, I don't think its best to use them here, I dont think all of these events are truly exceptional, perhaps the CURL could be, but not invalid usernames etc. hmmmm. – cecilli0n Mar 24 '14 at 00:13
  • Your programm/script needs to login in order to do whatever you want. If this login fails, you are not able to do anything with this script. Assuming this, exceptions thrown at this part fulfill their purpose. It's something which never should happen but it happened and you have to handle this situation. – 0x8BADF00D Mar 24 '14 at 06:43