I am working on a custom MVC inspired framework, mainly for learning purposes, and have gotten to the point where I need to create a better error handling class, I currently have a temp one just so i can see the errors better and save them to the database.
I have been reading about this subject for a few days now and need some clarification.
The errors i want to track are:
- System errors - if there is a problem with the actual code of the framework/app
- User input errors - forms for example, if they entered wrong information, or no information in required fields
What i want to do with the errors:
- Save them to the error log
- Save them to the database to an
- Display certain errors to the users and developers (for forms for example, or if they clicked a button that failed, they should see an alert/error message) based on a debug mode settings
- Show form input errors, if a user didn’t enter the correct data in to a form input, i want that input error to be saved so that i can display the correct error next to the correct input
My current error handling:
Not much to say here i think as it was just temporary, but what i have is really simple and poor class that i wrote just to be able to see the errors in a better way. I have been testing different things in it also so its not really working as it should. But basically the way i have it is like this:
setError() is the main function that is used, it receives the error lever and the error message, this function then sends it to another function that saves the error in the session and in the db and to another function that sends an email about the error.
I also have another function that saves input errors, for example, if a user submitted an input with too many chars, an error is set for that input and is saved in the session, so when the user returns to that form (either through reload or ajax), i can get the input errors and show a message by the input.
There are a few more functions but that’s basically what i have at the moment, i’m trying to create a “real” error handler class now, so the current class doesn’t really matter to me.
- How should i implement this in an MVC type framework, i would think that i could set the
set_error_handler()function to use my custom
ErrorHandler()class before the framework initiates (before calling the routers and everything), and then any errors saved will use that class, is this correct?
- How should i track user input errors? If i have a form input that should be an email address, and the user enters something that is not an email or the email is already taken or any other form input error, should i use
trigger_errorfor it? What error code should i give it?
- I haven’t fully understood where i should be using try, catch, and throw. What i do at the moment is use if statements where ever i think there could be an error. For example, if i try to insert data in to a table and it fails, i return false to which ever controller asked for this, and that controller would set an error (in my current temporary error handler) and save it without using try, throw, or anything like that, it would use a function i wrote called
setError(), and this would save it to the database and session for showing the users a message.