adlnet / Moodle-mod_cmi5launch

A Moodle plugin which allows teachers to upload cmi5 packaged lessons within a Moodle Course Activity and then assign the activity to students
Apache License 2.0
5 stars 4 forks source link

Better debugging #17

Closed ADLMeganBohland closed 1 year ago

ADLMeganBohland commented 1 year ago

Florian and I noticed that when the plugin has difficulty connecting to either the LRS or the CMI5 player, the error message is not very specific. IT is hard to decipher the issue if the user is not very familiar with the backend.

I have started implementing more specific error messages. Most of the connections to these external systems are done with file_get_contents. The issue is that this does not throw an error or exception on not working, only an E_Warning. To capture and display this warning to the user, we need to create our own error handler, and be sure to restore it once done. I have an example here in a try/catch block, with some extra error messages added for help to the user:

try { // File_get_contents throws a warning not error, so we need a specific handler to catch and alert user. set_error_handler(function ($severity, $message, $file, $line) { throw new \ErrorException($message, $severity, $severity, $file, $line); });

        $result = file_get_contents($url, false, $context);

    } catch (Exception $e) {

        echo"<br>";
        echo "Error connecting to LRS.";
        echo "<br>";
        echo "Trying to connect to LRS URL " . $url; 
        echo "<br>";
        echo "Be sure to check username and password for LRS in settings as well. ";
        echo "<br>";

        echo 'Caught exception. Error message from LRS is: ',  $e->getMessage(), "\n";
           // Print that it is missing.
           restore_error_handler();

    }
    restore_error_handler();

I will work on adding similar catches to other instances where file_get_contents might throw a warning.

ADLMeganBohland commented 1 year ago

I merged this branch into dev, changes complete.