I am wondering how to do this CORRECTLY, rather than one of the 40 ways I've done it in the past.
What is the best practice for storing/displaying system messages in a PHP application, I was just throwing a new Exception but it isnt working. I have in the past just stored the error/success messages in a session var, then when it gets displayed I just unset it.. But using global variables is a bad practice no?
So any advice on creating, handling, storing, and displaying error/success would be greatly appreciated!
Global application messages
Moderator: General Moderators
-
msurabbott
- Forum Commoner
- Posts: 33
- Joined: Thu Jan 01, 2009 10:18 pm
- Location: Chicago, IL, USA
Re: Global application messages
This is a good question!
I would say that there are A LOT of applications out there which are really bad at handling and displaying errors. I don't think there exists one "correct" way of doing this, but here are some thoughts of mine:
One should distinguish between error messages shown to the user and "technical" error messages generated by php or your code. For instance, the error message "Access denied for user: user@domain.com' (Using password: YES)" (when mysql_connect fails) would make no sense to the user. On the other hand, this is very useful for you as a developer. So a two-tire approach might be an idea.
For the developer, create a log class which logs all techical errors in a text file (or database) for the developer to read. In my view, this class can be put in the session, even though global variables is a bit of a no-no. This is because error handling by all means is a global thing. Alternatively, you can create an instance of the class every time an error occurs, but it you store the errors in a file, this could cause a lot of opening and closing of the file.
For the user, create a sensible error message to display to the user, such as "Due to a temporary techical problem, the service is currently unavailable. Please try again in a few minutes". The error messages shown to the user can be stored in a sepparate file (using DEFINE) or even a database. Thus, you have a sepparation between logic (your code) and layout (since language strictly speaking is a part of layout, not program logic). (The techical error messages can be hard-coded, but should not be shown to the user.) Displaying errors can simply be done by using a common css for all errors.
This is one approach, there are probably lots of others. It would be nice if more people came with some thoughs around this.
I would say that there are A LOT of applications out there which are really bad at handling and displaying errors. I don't think there exists one "correct" way of doing this, but here are some thoughts of mine:
One should distinguish between error messages shown to the user and "technical" error messages generated by php or your code. For instance, the error message "Access denied for user: user@domain.com' (Using password: YES)" (when mysql_connect fails) would make no sense to the user. On the other hand, this is very useful for you as a developer. So a two-tire approach might be an idea.
For the developer, create a log class which logs all techical errors in a text file (or database) for the developer to read. In my view, this class can be put in the session, even though global variables is a bit of a no-no. This is because error handling by all means is a global thing. Alternatively, you can create an instance of the class every time an error occurs, but it you store the errors in a file, this could cause a lot of opening and closing of the file.
For the user, create a sensible error message to display to the user, such as "Due to a temporary techical problem, the service is currently unavailable. Please try again in a few minutes". The error messages shown to the user can be stored in a sepparate file (using DEFINE) or even a database. Thus, you have a sepparation between logic (your code) and layout (since language strictly speaking is a part of layout, not program logic). (The techical error messages can be hard-coded, but should not be shown to the user.) Displaying errors can simply be done by using a common css for all errors.
This is one approach, there are probably lots of others. It would be nice if more people came with some thoughs around this.
- The_Anomaly
- Forum Contributor
- Posts: 196
- Joined: Fri Aug 08, 2008 4:56 pm
- Location: Tirana, Albania
Re: Global application messages
Pardon my apparent ignorance, but what exactly do you mean by Global Application Messages? The errors that you see for a fatal error or exception? Server errors? Some way to send messages to every user on the site?
-
msurabbott
- Forum Commoner
- Posts: 33
- Joined: Thu Jan 01, 2009 10:18 pm
- Location: Chicago, IL, USA
Re: Global application messages
The_Anomaly, pkbruker hit the nail on the head as to what I am looking for... A way to handle ALL types of errors that a PHP application would encounter. This means technical for developer, and non-technical for users. What PHP methods to use, throw/catch, trace, etc..
pkbruker, I am good with how to display the error, I suppose my question should have been how, using what I have stored in db, file, or session, would I display the error. So more or less how should I go about retrieving the error. Is this something that you would just add to the db with a flag 'been_displayed' and at the load of every page just check the db, and display all messages that have yet to be displayed?
The messages I am attempting to display are messages like 'Bad password/username', 'success/error on saving profile information'.. stuff like that.
As far as technical errors go shouldn't a developer always throw exception when errors occur, in order to stop execution of the script, or should I be writing the technical errors to the db, then just using a return; statement or something?
pkbruker, I am good with how to display the error, I suppose my question should have been how, using what I have stored in db, file, or session, would I display the error. So more or less how should I go about retrieving the error. Is this something that you would just add to the db with a flag 'been_displayed' and at the load of every page just check the db, and display all messages that have yet to be displayed?
The messages I am attempting to display are messages like 'Bad password/username', 'success/error on saving profile information'.. stuff like that.
As far as technical errors go shouldn't a developer always throw exception when errors occur, in order to stop execution of the script, or should I be writing the technical errors to the db, then just using a return; statement or something?