Jump to content

Error handling and exceptions


shino

Recommended Posts

Hi what I am trying to accomplish is this:

 

Say you are a user and you are presented a webpage with a nice layout and a form to fill in, and you fill out the information and miss some required fields, what i would do then is validate the input and present an error msg besides each required field. That is fine for me.

 

Second is if something breaks within my code due to whatever reason, instead of outputting an error outside of the html and causing the layout to break I would prefer to be able to output a nice error msg within the website's layout surrounded by whatever CSS I would put.

 

Third, if a serious fatal error would occur, instead of just stopping the script I would probably create a specific error page which would have the website's layout but contain a specific error so it would be both presentable to the user seeing the error and contain the information I would need.

 

My question is this, how would you or anyone if you guys have better understanding of handling errors and exceptions, achieve this?

I mean for the error handling part (Excluding exceptions), most examples I come accross the internet, just show notices and warnings like the usual php errors, but what I want is to be able to take that error, maybe store it in a variable and output it anywhere I want on a webpage so that way I can truly customize the look when an error occurs and not simply just break the whole website's design because an error occured.

 

Sorry if im not being clear enough. Any opinions or advice is welcome!

Link to comment
Share on other sites

Like this?

 

$errors = array();
if (empty($_POST['username'])) {
  $errors[] = 'You stupid F*$% can\'t you read? It said REQUIRED as in I NEED IT???';
}

 

No that I understand about validation, that is no problem at all.

 

It's more like this.

 

<?php
function login($username, $password) {
// do login check but for some reason who knows what it could be its possible something fails and not the validation but more the function calls or w/e
// then error or crash
   throw new Exception('Something happened and it crashed!');
}

if ($_SERVER['REQUIRED_METHOD'] == 'POST') {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

   if (!empty($username) && !empty($password)) {
        try {         
               // do login check
              $check_login = login($username, $password);
        } catch (Exception $e) {
              $errors = $e->getMessage();
        }
   }
}
?>
<html>
<head></head>
<body>
SOME HEADINGS OR TEXT HERE
<?php 
      if (!empty($errors)) { echo $errors; }
?>
FORM GOES HERE
</body>
</html>

 

What I basically want to achieve is something similar to what I quickly wrote above, it might not be ideal but it's something like that that I want to be able to produce. Basically any error which could happen for whatever reason, be outputted in the html content so that it can appear friendly to the user but can at least warn them something happened and if by any chance they would have filled info on their forms or w/e we could still salvage it so they can at least maybe save their info without having to retype it or i don't know but it's what Im hoping to achieve without having a blank page with errors or errors outputted before html, etc.

 

I'm just looking for your professional opinions or tips on handling errors like these, not just exceptions.

Validation is ok, it's more like other errors that would make the functions fail or etc.

 

Sorry if im still not clear, Im not good at explaining... lol

Link to comment
Share on other sites

How do you mean then, something like this?

 

try {
  //something
} catch (UnknownCredentialsException $e) {
  $errors[] = 'You are getting warmer..';
} catch (DatabaseUnavailableException $e) {
  $errors[] = 'Great, you fried the system. Are you happy now?';
}

 

Yes exactly, and being able to output that where and when an error occurs anywhere on the page.

Don't know if that's a good method of handling errors, if there is a preferable one, or any input, ideas or opinions on doing it this way.

 

It would be nice if something like this would work

 

<?php
if (!empty($errors)) { echo $errors; }

// this function will fail with an error
// but the error would be outputted above.
call_some_function();
?>

Link to comment
Share on other sites

<?php
if (!empty($errors)) { echo $errors; }

// this function will fail with an error
// but the error would be outputted above.
call_some_function();
?>

 

Doesn't work, however:

 

<?php
// this function will fail with an error
// but the error would be outputted above.
$errors = call_some_function();
if (!empty($errors)) { echo $errors; }
?>

 

Will.

Link to comment
Share on other sites

Thanks for your replies, now I just need to know what you guys think about handling errors like this.

Have any of you done so, do you find it works well and makes the handling of errors more friendly to the users whom might experience them, what are your thoughts on this? You might take this in context versus a notice or warning appearing on very top of your browser page, or crashing the program itself with a blank page and some wierd error, etc.

Link to comment
Share on other sites

A warning or a notice is never a good error handling especially because a production server has display_errors = Off which will hide your error messages to the user. I also don't believe exceptions is a good way to go. Exceptions are also a bad idea because an unhandled exception will leave your website in an unusable state.

 

Your ultimate goal should be - no matter what - to keep the website in a usable state. Personally though I prefer to use Zend_Validator & Zend_Form.

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.