28 April 2009

Gawayne and the Green Knight

I finished another good story recently. This story I listened to instead of read. During my morning workouts I have started listening to stories because, as far as I can tell, either cable providers are completely disconnected to their audience or I am not their audience. Sixty channels of trash was too much for me so now I do my best not to look at the filth on the screen at the gym and focus on a good story instead. I find the stories I listen to on the Librivox website.

Gawayne and the Green Knight is the story Sir Gawayne, a knight of King Aurthur's court. It is a short story written as an old english rhyme in the late 14th-century. I read the modern adaptation by Charlton Miner Lewis. It was a fun story with all the joys of Arthurian legend. There were knights, damsels, and of course a quest. The story was fun and enjoyable to read.

At one point where Sir Gawayne's virtue is tested to the utmost he states the following:
Love? What is love? Not the wild feverish thrill,
When heart to heart the thronging pulses fill,
And lips that close in parching kisses find
No speech but those;--the best remains behind.
The tranquil spirit--the divine assurance
That this life's seemings have a high endurance--
Thoughts that allay this restless striving, calm
The passionate heart, and fill old wounds with balm;--
These are the choirs invisible that move
In white processionals up the aisles of love.
He strikes a point that has been largely forgotten in the world today. Love is deeper than infatuation. Love is more enduring than passion. In our world of quick fixes and fast satisfaction love is being lost. Marriages end with the first disagreement. Families are broken by the unfocused lust for pleasure. There needs to be a return to love being that of healing wounds, calming fears, enduring both pain and pleasure. That is love.

12 February 2009

PHP Errors and Exceptions

I learned something about php error handling the other day. I was digging through the docs and discovered how to catch errors, warnings, and such and then re-throw them as exceptions. This is great because I already implemented a default exception handling and logging system to catch all exceptions. Now I can use the same routines for PHP errors. The keys to making this work are the set_error_handler function and the ErrorException class. The set_error_handler has been around for a long time, but I have never really used it. This is a failing on my part. The ErrorException class seems to have been made especially for this type of operation. The ErrorException documentation shows exactly how to use these two things together for object oriented goodness in PHP.
<?php
  function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  }
  set_error_handler("exception_error_handler");
?>
The function name passed to set_error_handler will be called if there is ever a PHP error. The function set up to handle errors takes the error information, constructs an ErrorException and then throws it. Errors never go directly out to the user or logs, they are always wrapped in an exception. Doing this give you greater control over error handling.

In the user comments on the ErrorException page troelskn gives a suggestion for improvement to the basic example above. He suggests the following code.
<?php
function exception_error_handler($severity, $message, $filename, $lineno) {
  if (error_reporting() == 0) {
    return;
  }
  if (error_reporting() & $severity) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
  }
}
set_error_handler('exception_error_handler');
?>
This code allows the error reporting value set in php.ini to effect the behavior of the exception_error_handler. Only errors of a high enough severity will be passed on as exceptions. Using his code, if you have warnings suppressed, they will not raise an exception. They will just not be reported. I hope this removes one more of the little PHP quarks for you.