8. Error Handling
Activity 8.1: Improving the User Experience through the Handling System and User-Level Errors
Solution
- Create a file called
factorial.php
. - First, add the exception handler that, in order to log the exceptions to the log file, will create a data stream resource using the
fopen()
function, which is assigned to the static variable,$fh
:$exceptionHandler = function (Throwable $e) { Â Â Â Â static $fh; Â Â Â Â if (is_null($fh)) { Â Â Â Â Â Â Â Â $fh = fopen(__DIR__ . '/app.log', 'a'); Â Â Â Â Â Â Â Â if (!$fh) { Â Â Â Â Â Â Â Â Â Â Â Â echo 'Unable to access the log file.', PHP_EOL; Â Â Â Â Â Â Â Â Â Â Â Â exit(1); Â Â Â Â Â Â Â Â } Â Â Â Â }
- Format the log message and write to the log...