Protecting from crash
I am not sure exactly what have I done, but suddenly, when I tried to access the development web server, it crashed with the following error:
Error open (<:raw:encoding(UTF-8)) on '/Users/gabor/work/MetaCPAN-SCO/totals.json': No such file or directory at lib/MetaCPAN/SCO.pm line 59.
Right, so apparently I have removed the totals.json form the root of the application and when I tried to load the main page it could not find the file and died when the object returned by path tried open in for reading. We should either make lots of checks: Does the file exist? Can it be opened? Is the content JSON? Can it be converted to Perl data structure? Or, we should wrap the whole thing in an eval-block and check if we have received an exception from either the open or from the from_json.
Actually, at least for now, it is enough if we wrap the call in an eval-block. After all, there is not much we can do if this fails. Maybe we should see it in a log file, but I am not sure that the web application should do that. If this failure is caused by an unlucky timing when the file was updated by the cron-job, then it will be fixed within a second, and if it is a permanent issue then we probably don't want our log-file to be filled with this error message.
So we only write:
eval { $vars->{totals} = from_json path("$root/totals.json")->slurp_utf8; };
Published on 2015-05-29