# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: moodle/admin/settings/appearance.php
--- moodle/admin/settings/appearance.php Base (1.70)
+++ moodle/admin/settings/appearance.php Locally Modified (Based On 1.70)
@@ -126,6 +126,8 @@
     $temp->add($setting);
     $temp->add(new admin_setting_configcheckbox('enablecourseajax', get_string('enablecourseajax', 'admin'),
                                                 get_string('enablecourseajax_desc', 'admin'), 1));
+    $temp->add(new admin_setting_configcheckbox('catchjserrors', get_string('catchjserrors', 'admin'),
+                                                get_string('catchjserrors_desc', 'admin'), 1));
     $ADMIN->add('appearance', $temp);
 
     // link to tag management interface
Index: moodle/lang/en/admin.php
--- moodle/lang/en/admin.php Base (1.142)
+++ moodle/lang/en/admin.php Locally Modified (Based On 1.142)
@@ -88,6 +88,8 @@
 $string['cannotdeletemodfilter'] = 'You cannot uninstall the \'{$a->filter}\' because it is part of the \'{$a->module}\' module.';
 $string['cannotdeleteqtypeinuse'] = 'You cannot delete the question type \'{$a}\'. There are questions of this type in the question bank.';
 $string['cannotdeleteqtypeneeded'] = 'You cannot delete the question type \'{$a}\'. There are other question types installed that rely on it.';
+$string['catchjserrors'] = 'Attempt to catch JS exceptions';
+$string['catchjserrors_desc'] = 'When enabled JS init and function calls added to the page by Moodle will be wrapped in try...catch statements. This helps reduce the complete JS failures and should only be turned off for debugging.';
 $string['cfgwwwrootslashwarning'] = 'You have defined $CFG->wwwroot incorrectly in your config.php file. You have included a \'/\' character at the end. Please remove it, or you will experience strange bugs like <a href=\'http://tracker.moodle.org/browse/MDL-11061\'>MDL-11061</a>.';
 $string['cfgwwwrootwarning'] = 'You have defined $CFG->wwwroot incorrectly in your config.php file. It does not match the URL you are using to access this page. Please correct it, or you will experience strange bugs like <a href=\'http://tracker.moodle.org/browse/MDL-11061\'>MDL-11061</a>.';
 $string['clamfailureonupload'] = 'On clam AV failure';
Index: moodle/lib/javascript-static.js
--- moodle/lib/javascript-static.js Base (1.173)
+++ moodle/lib/javascript-static.js Locally Modified (Based On 1.173)
@@ -1606,3 +1606,35 @@
             }
     });
 };
+
+/**
+ * This function handles exceptions that get throw by init scripts in the page.
+ * 
+ * All JavaScript that gets added to the page by the executing Moodle code will
+ * be wrapped in a try...catch that passes any exceptions here.
+ * You can disable the addition of the try...catch by turning off
+ * $CFG->catchjserrors.
+ * 
+ * Exceptions that get caught by this function get displayed at the top of the 
+ * screen provided developerdebug is on, otherwise just a class gets added to
+ * the body.
+ * 
+ * @param {object} ex
+ */
+M.util.handle_init_exception = function(ex) {
+    if (M.cfg && M.cfg.developerdebug) {
+        var divid = 'js-init-exception';
+        var div = document.getElementById(divid);
+        if (!div) {
+            div = document.createElement('div');
+            div.setAttribute('id', divid); 
+            div.innerHTML = '<span class="title">JavaScript Exception:</span> '+ex.message;
+            document.body.appendChild(div);
+        } else {
+            div.appendChild(document.createTextNode(', '+ex.message));
+        }
+    } 
+    if (!/js\-exception\-occured/.test(document.body.className)) {
+        document.body.className += ' js-exception-occured'; 
+    }
+}
\ No newline at end of file
Index: moodle/lib/outputrequirementslib.php
--- moodle/lib/outputrequirementslib.php Base (1.58)
+++ moodle/lib/outputrequirementslib.php Locally Modified (Based On 1.58)
@@ -926,6 +926,7 @@
      * @return bool $ondomready
      */
     protected function get_javascript_code($ondomready) {
+        global $CFG;
         $where = $ondomready ? 'ondomready' : 'normal';
         $output = '';
         if ($this->jscalls[$where]) {
@@ -935,7 +936,10 @@
             if (!empty($ondomready)) {
                 $output = "    Y.on('domready', function() {\n$output\n    });";
             }
+            if (!empty($CFG->catchjserrors)) {
+                $output = $this->add_js_exception_try_catch($output);
         }
+        }
         return $output;
     }
 
@@ -944,13 +948,31 @@
      * @return unknown_type
      */
     protected function get_javascript_init_code() {
+        global $CFG;
         if (count($this->jsinitcode)) {
-            return implode("\n", $this->jsinitcode) . "\n";
+            $initcode = $this->jsinitcode;
+            if (!empty($CFG->catchjserrors)) {
+                $initcode = array_map(array(__CLASS__, 'add_js_exception_try_catch'), $initcode);
         }
+            return implode("\n", $initcode) . "\n";
+        }
         return '';
     }
 
     /**
+     * Wraps the provided JS code in a try catch that attempts to handle any
+     * exceptions by passing them to M.util.handle_init_exception();
+     *
+     * @param string $jscode
+     * @return string 
+     */
+    protected function add_js_exception_try_catch($jscode) {
+        $handle  = 'if (M.util && M.util.handle_init_exception) { M.util.handle_init_exception(ex); }';
+        $jscode = "try {\n$jscode\n} catch (ex) {\n$handle\n}";
+        return $jscode;
+    }
+
+    /**
      * Returns basic YUI3 JS loading code.
      * YUI3 is using autoloading of both CSS and JS code.
      *
Index: moodle/theme/base/style/core.css
--- moodle/theme/base/style/core.css Base (1.78)
+++ moodle/theme/base/style/core.css Locally Modified (Based On 1.78)
@@ -704,3 +704,7 @@
 .mod-indent-14 {margin-left:280px;}
 .mod-indent-15,
 .mod-indent-huge {margin-left:300px;}
+
+.js-exception-occured {margin-top:15px;}
+.js-exception-occured #js-init-exception {text-align:center;background-color:#FFDDDD;border:1px solid #FFDDDD;border-bottom-color:#660000;color:#660000;padding:5px 4px 4px 4px;font-size:10px;position:fixed;top:0;left:0;width:100%;height:15px;}
+.js-exception-occured #js-init-exception .title {font-weight:bold;}
\ No newline at end of file
