1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            Portions ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 /**
  9   @namespace
 10 
 11   If an exception is thrown during execution of your SproutCore app, this
 12   object will be given the opportunity to handle it.
 13 
 14   By default, a simple error message is displayed prompting the user to
 15   reload. You could override the handleException method to, for example, send
 16   an XHR to your servers so you can collect information about crashes in your
 17   application.
 18 
 19   Since the application is in an unknown state when an exception is thrown, we
 20   rely on JavaScript and DOM manipulation to generate the error instead of
 21   using SproutCore views.
 22 
 23   @since SproutCore 1.5
 24 */
 25 SC.ExceptionHandler = {
 26 
 27   /** @private */
 28   enabled: (SC.buildMode !== 'debug'),
 29 
 30   /**
 31     Called when an exception is encountered by code executed using SC.run().
 32 
 33     By default, this will display an error dialog to the user. If you
 34     want more sophisticated behavior, override this method.
 35 
 36     @param {Exception} exception the exception thrown during execution
 37   */
 38   handleException: function(exception) {
 39     if (this.isShowingErrorDialog) return NO;
 40     
 41     this._displayErrorDialog(exception);
 42     
 43     return NO;
 44   },
 45 
 46   /** @private
 47     Creates the error dialog and appends it to the DOM.
 48 
 49     @param {Exception} exception the exception to display
 50   */
 51   _displayErrorDialog: function(exception) {
 52     var html = this._errorDialogHTMLForException(exception),
 53         node = document.createElement('div');
 54 
 55     node.style.cssText = "left: 0px; right: 0px; top: 0px; bottom: 0px; position: absolute; background-color: white; background-color: rgba(255,255,255,0.6); z-index:100;";
 56     node.innerHTML = html;
 57 
 58     document.body.appendChild(node);
 59 
 60     this.isShowingErrorDialog = YES;
 61   },
 62 
 63   /** @private
 64     Given an exception, returns the HTML for the error dialog.
 65 
 66     @param {Exception} exception the exception to display
 67     @returns {String}
 68   */
 69   _errorDialogHTMLForException: function(exception) {
 70     var html;
 71 
 72     html = [
 73 '<div id="sc-error-dialog" style="position: absolute; width: 500px; left: 50%; top: 50%; margin-left: -250px; background-color: white; border: 1px solid black; font-family: Monaco, monospace; font-size: 9px; letter-spacing: 1px; padding: 10px">',
 74   'An error has occurred which prevents the application from running:',
 75   '<br><br>',
 76   exception.message,
 77   '<div id="sc-error-dialog-reload-button" onclick="window.location.reload();" style="float: right; font-family: Monaco, monospace; font-size: 9px; letter-spacing: 1px; border: 1px solid black; padding: 3px; clear: both; margin-top: 20px; cursor: pointer;">',
 78   'Reload',
 79   '</div>',
 80 '</div>'
 81     ];
 82 
 83     return html.join('');
 84   },
 85 
 86   /**
 87     YES if an exception was thrown and the error dialog is visible.
 88 
 89     @type Boolean
 90     @default NO
 91   */
 92   isShowingErrorDialog: NO
 93 };