shino Posted May 29, 2010 Share Posted May 29, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/203288-error-handling-and-exceptions/ Share on other sites More sharing options...
ignace Posted May 29, 2010 Share Posted May 29, 2010 Like this? $errors = array(); if (empty($_POST['username'])) { $errors[] = 'You stupid F*$% can\'t you read? It said REQUIRED as in I NEED IT???'; } Quote Link to comment https://forums.phpfreaks.com/topic/203288-error-handling-and-exceptions/#findComment-1065093 Share on other sites More sharing options...
shino Posted May 29, 2010 Author Share Posted May 29, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/203288-error-handling-and-exceptions/#findComment-1065102 Share on other sites More sharing options...
ignace Posted May 29, 2010 Share Posted May 29, 2010 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?'; } Quote Link to comment https://forums.phpfreaks.com/topic/203288-error-handling-and-exceptions/#findComment-1065109 Share on other sites More sharing options...
shino Posted May 29, 2010 Author Share Posted May 29, 2010 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/203288-error-handling-and-exceptions/#findComment-1065111 Share on other sites More sharing options...
ignace Posted May 29, 2010 Share Posted May 29, 2010 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/203288-error-handling-and-exceptions/#findComment-1065114 Share on other sites More sharing options...
shino Posted May 29, 2010 Author Share Posted May 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203288-error-handling-and-exceptions/#findComment-1065125 Share on other sites More sharing options...
ignace Posted May 29, 2010 Share Posted May 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203288-error-handling-and-exceptions/#findComment-1065152 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.