[nycphp-talk] Can I set the response code if a PHP Fatal error occurs?
David Krings
ramons at gmx.net
Tue Feb 17 11:55:57 EST 2009
Michael Sims wrote:
> Your Javascript code should look like this:
>
> a) did I get the right data back from the server? If so, report success.
>
> b) Otherwise, report failure to the user.
>
> It doesn't matter what went wrong on the server side, if you're just
> reporting to the user. MAke your PHP script return something (the word
> "good" for example), have your javascript look for that word, if it
> didn't get that word, report to the user "Something seems to be wrong".
>
> Behind the scenes it might matter to you what went wrong with the PHP
> script, but it doesn't matter to the user at all. You shouldn't be
> testing the http status code as a way to figure out if things were
> successful - test some data output by the PHP script, where that data is
> only sent if everything went correctly.
Working now in QA and having had years of experience with support you do want
to report exactly what went wrong and what the user can do about it (if
possible). Implementing useless error messages is just bad advice and makes
for bad software. Make your PHP script determine what went wrong and show a
useful error message. At a minimum an error message must include:
- type of error
- reason for error
- what was received (if applicable)
- what was expected (if applicable)
- measures to prevent or workaround error or instructions to obtain additional
help (such as "Read the applicable error description in the help here: <link
to help topic>" or "Contact support at blahblah at blah.blah"). Ideally, provide
a solution right then and there.
Optionally report this:
- the name of the module / script in which the error occured
- additional information such as server and PHP version or custom error codes
Error messages must fulfill three tasks:
- appear when otherwise the application would not continue to work correctly
or data loss can occur (for example, showing an error message is better than
just blue screen)
- provide information about what went wrong, why it went wrong, and what one
can do about it
- provide information that really helps the developer to fix the bug (if there
is one)
Messages like "Something seems to be wrong" are as useful as any other random
array of words. The user already noticed that something went wrong as the
outcome is not the one expected.
I know that proper error handling and reporting is a thankless, tedious task
that adds zero functionality to an application. It is boring work and every
developer hates it. It is also never complete. But it for sure helps everyone
who makes or uses the application. Not implementing proper error handling and
reporting is not an option.
David
More information about the talk
mailing list