Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-64961

REST web service: Fatal error in error handler

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • 3.5.5, 3.6.3
    • 3.5.4, 3.6.2, 3.7
    • Web Services
    • MOODLE_35_STABLE, MOODLE_36_STABLE, MOODLE_37_STABLE
    • MOODLE_35_STABLE, MOODLE_36_STABLE
    • MDL-64961-master
    • Hide

      Test as admin.

      1. Ensure web services are enabled on Advanced features / Enable web services, and the REST protocol is enabled on Plugins / Web services / Manage protocols
      2. Go to Plugins / Web services / External services; click Add at bottom
      3. Fill in form options: 'Stupid name', 'stupid', enabled; submit.
      4. Add function core_fetch_notifications.
      5. Go to Plugins / Web services / Manage tokens; click Add at bottom.
      6. Type 'admin' username, Select the stupid service; submit.
      7. Find the right token, copy and paste it into this URL:
      8. Edit code lib/external/externallib.php, function fetch_notifications. Insert this line at start of function:
        • throw new \Exception('Some other exception');
      9. Reload the URL above.
        • EXPECTED: You should get a JSON object containing 'Some other exception' text and other details. It does not have an error code.
        • BEFORE FIX: Depending on platform or PHP configuration, you either get an approximately correct JSON with errorcode set to 'null', or else you get a blank result with an HTTP status error. In the latter case, if you look in your Apache error log you will find an error like this: [Wed Feb 27 10:25:38.825009 2019] [php7:error] [pid 836:tid 968] [client 127.0.0.1:54012] PHP Fatal error: Uncaught Exception: Notice: Undefined property: Exception::$errorcode in (system path)
          webservice
          rest
          locallib.php on line 154 [...]
      Show
      Test as admin. Ensure web services are enabled on Advanced features / Enable web services, and the REST protocol is enabled on Plugins / Web services / Manage protocols Go to Plugins / Web services / External services; click Add at bottom Fill in form options: 'Stupid name', 'stupid', enabled; submit. Add function core_fetch_notifications. Go to Plugins / Web services / Manage tokens; click Add at bottom. Type 'admin' username, Select the stupid service; submit. Find the right token, copy and paste it into this URL: https://YOUR.MOODLE.ROOT/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=core_fetch_notifications&contextid=1&wstoken=YOURTOKEN The URL should work and return [] (empty JSON array), assuming you don't have any notifications Edit code lib/external/externallib.php, function fetch_notifications. Insert this line at start of function: throw new \Exception('Some other exception'); Reload the URL above. EXPECTED: You should get a JSON object containing 'Some other exception' text and other details. It does not have an error code. BEFORE FIX: Depending on platform or PHP configuration, you either get an approximately correct JSON with errorcode set to 'null', or else you get a blank result with an HTTP status error. In the latter case, if you look in your Apache error log you will find an error like this: [Wed Feb 27 10:25:38.825009 2019] [php7:error] [pid 836:tid 968] [client 127.0.0.1:54012] PHP Fatal error: Uncaught Exception: Notice: Undefined property: Exception::$errorcode in (system path) webservice rest locallib.php on line 154 [...]

      An error handler in REST web services is supposed to turn exceptions into nice JSON format error messages. This code works correctly only for moodle_exception; for other exceptions, on certain platforms/PHP settings (on my Windows PC with PHP 7.2; not on my RHEL developer server with PHP 7.1), it causes a fatal error because it tries to access the 'errorcode' property which does not exist..

      As Moodle code mainly only throws moodle_exception this is usually not a problem but it can happen if Moodle code calls library code that may throw an exception; also, in PHP 7+, various system errors are now thrown as exceptions/errors.

      This can make it harder to diagnose problems e.g. in the mobile app (or anything else calling REST web services) because the returned value doesn't contain any mention of the exception, and nor does the server log; it just logs the fatal error...

      I propose changing it to behave the same as debuginfo, so that if there is no errorcode, it doesn't get included in the JSON value.

        1. MDL-64961.png
          36 kB
          Jennifer Bauzon

            quen Sam Marshall
            quen Sam Marshall
            Mark Johnson Mark Johnson
            Andrew Lyons Andrew Lyons
            Jennifer Bauzon Jennifer Bauzon
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 2 hours
                2h

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.