0

I am utilizing Codeigniter.

I have developed a variety of features, and they work perfectly if used as intended.

One such script, a whois script checks the owner of a domain name. If however the user types in an invalid domain name, all sorts of errors are being thrown up here there and everywhere.

For example, if a user types in stack.-com, this is of course not a valid domain. Thus when i call my helper which does the query, no result is return and a variety of errors are returned. There are also errors when i try to display an empty array to the user.

My question relates to errors.

I could use preg_match and check if the domain is valid. If not i set an error variable which i intend to output to the user.

However before my controller gets to the if else statement which decides whether to show the error page or the results page, the program is running queries, and accessing methods to get the data which were there no errors would get the data to pass to the result view.

I.E I know there is an error, but still lots of other errors are being shown because an invalid item is being passed to my other scripts.

With Codeigniter, and the MVC setup, what is the best way of catching the error and displaying it to the user without having to use exceptions which do the same thing over and over again?

Thanks

EDIT WITH IDEA

try
{
$this->load->model('whois');
$this->whois->process('domain.-com');
}
catch
{
$this->load->view('errors',$errordata);
$this->load->view('footer');
die;
}

$this->load->view('normal_content');
$this->load->view('footer');

Is this the suggested setup for using exceptions with codeigniter? within my model, the function will throw exceptions if there is a problem. The catch statement will then display them and die, thus not showing the content.. It does not seem right..?

Thomas Clowes
  • 4,529
  • 8
  • 41
  • 73
  • 3
    Normally you either check sanity of the user input in the controller (if it is just a matter about formatting that could be checked using preg_match()) or make sure your models have sufficient error handling and throw exceptions when they are asked to perform something illegal. – Niklas Lindblad Jan 05 '12 at 23:53
  • 2
    Why would you not want to use exceptions? You might wanna look at http://stackoverflow.com/questions/6217786/codeigniter-and-throwing-exceptions – Niklas Lindblad Jan 05 '12 at 23:55
  • Edited above. Still a little confused. Thx – Thomas Clowes Jan 06 '12 at 00:25
  • That seems like a better way of handling the exception, yes. – Niklas Lindblad Jan 06 '12 at 00:27

2 Answers2

1

Here's the way I usually handle this:

  1. Post your form back to the same route
  2. If there are errors, show the form again, with an error state
  3. If everything passes, redirect NOW to the next step / success state

Sample code:

<?php
...

public function form()
{
    if (strtoupper($this->input->server('REQUEST_METHOD')) == 'POST')
    {
        try {
            // handle all your validation here
            redirect('success_route');
            exit;
        }
        catch (Exception $e)
        {
            // this could get a little fancier, but a simple solution is to pass the exception
            // directly to the view.  you could also load the `errors` view here, but return the
            // content to a variable and pass to your full view
            $this->load->vars('exception', $e);
        }
    }

    $this->load->view('normal_content');
    $this->load->view('footer');
}

...
landons
  • 9,502
  • 3
  • 33
  • 46
  • Does CodeIgniter have any way to route uncaught exceptions to a 'error' controller? – Tim Lytle Jan 08 '12 at 00:08
  • Not by default. You'd have to extend CI_Controller or register an exception handler. The difficult part is that you can't load an additional controller if the first one threw an exception. Pretty much have to die or redirect from there... – landons Jan 08 '12 at 00:12
  • Ah. Seems like that would be a nice feature. – Tim Lytle Jan 08 '12 at 01:07
  • Yeah. It's a little married to a specific implementation, which is probably why they don't have it. That doesn't bode well for a framework. I doubt it would be hard with a couple of hooks and overriding the exceptions class... – landons Jan 08 '12 at 01:21
0

You have to do as follows

1) database.php : $db['default']['db_debug'] = FALSE;

2) in your modal file

try {
   $query_str = "SELECT * FROM `pro_property` WHERE username = '".$username."'";
   $result = $this->db->query($query_str);

   if (!$result)
   {
       throw new Exception('error in query');
       return false;
   }        

   return $result;

} catch (Exception $e) {
    print_r($e);
}