Bookmark and Share

Friday, January 29, 2010

Dealing fatal errors in PHP

When a fatal error in PHP occurs, the flow of the program halts resulting in a blank screen or a system error message. Leaving your end-users confused and you, as the programmer, unaware that these fatal errors might be occuring.

RawDev offers a simple way to handle fatal errors more gracefully so that you can (a) log/email the error and (b) display an appropriate message and a path to continue so that the end-user is not staring at a system message or worse, a blank screen unsure what to do next.


How it works

PHP displays the fatal error right before it halts. RawDev reads the output buffer and detects when a fatal error occurs. That's when it calls your custom handler.

Basically all you have to do is register a fatal error handler. See example below.

Example


RFatal::setHandler('fatalErrorHandler');

hello(); # since this function does not exist it will throw a fatal error

# this area is never reached because you get a handle, but PHP is going to halt anyways

RFatal::unsetHandler(); #optional function to remove the fatal error handler

?>

Display Errors


Note that display_errors needs to be set for the RFatal class to work, which is the default in PHP. Usually this config parameter is set by the sysadmin in the php.ini file. Sometimes (typically in production) this is turned off for security reasons with non-favorable usability impact. In RawDev this needs to be turned on. The security issue no longer applies because an appropriate message to the end-user should be displayed by you, the programmer, in the fatal error handler. Typically something similar to a 404 with a simple message and a path to continue.



Conclusion

By trapping the output buffering you can actually get a handle to fatal errors, and RawDev makes it easy to do this. There are cases where you do not want to do output buffering, for example when dealing with very large output. In that case, don't use fatal error trapping.



Links

API Doc for RFatal

2 comments:

  1. The Best 10 Casinos in San Francisco - Mapyro
    › Sanfrancisco › Best-10 › Sanfrancisco › 청주 출장안마 Best-10 Casinos in San Francisco - Mapyro: Top 상주 출장샵 Casino & Hotel 영주 출장마사지 Hotels Nearby! Find reviews and 경주 출장마사지 discounts for AAA/AARP members, seniors, 부천 출장안마

    ReplyDelete
  2. For on-line on line casino gamblers, people reported taking part in} blackjack (78%) most often, followed by video poker (65%), slot machines (60%), roulette (37%), and craps (29%). The ahead for} Internet gambling in the United States 온라인 카지노 stays unsure. Under the Unlawful Internet Gambling Enforcement Act of 2006 banks and credit card companies are committing legal offense|against the law} occasion that they} switch Americans' money to Internet gambling websites. Though critical Internet gamblers will likely discover methods of transferring funds to on-line casinos and card rooms, lawmakers hope the law will turn casual gamblers away. As of late 2006, lots of the larger, publicly traded Internet gambling companies, such as PartyPoker, had stopped accepting American customers altogether to avoid any conflicts with the united states government. However, the legality of Internet gaming stays somewhat unsure.

    ReplyDelete