[gjs] Fix JSUnit to use regular exceptions objects



commit 61d3d45914458998078d2b31011a54847decb35f
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Tue May 7 12:55:01 2013 +0200

    Fix JSUnit to use regular exceptions objects
    
    This way we can assume that all exceptions stringify to something
    useful and have a valid .stack property, which is an assumption
    we want to make for the new error logger.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=690984

 modules/jsUnit.js |   46 ++++++----------------------------------------
 1 files changed, 6 insertions(+), 40 deletions(-)
---
diff --git a/modules/jsUnit.js b/modules/jsUnit.js
index 08f7a19..6fc7cf5 100644
--- a/modules/jsUnit.js
+++ b/modules/jsUnit.js
@@ -75,14 +75,11 @@ function _displayStringForValue(aVar) {
 }
 
 function fail(failureMessage) {
-  throw new JsUnitException(null, failureMessage);
+    throw new JsUnitException(null, failureMessage);
 }
 
 function error(errorMessage) {
-  var errorObject         = new Object();
-  errorObject.description = errorMessage;
-  errorObject.stackTrace  = getStackTrace();
-  throw errorObject;
+    throw new Error(errorMessage);
 }
 
 function argumentsIncludeComments(expectedNumberOfNonCommentArgs, args) {
@@ -231,38 +228,6 @@ function getFunctionName(aFunction) {
   return name;
 }
 
-function getStackTrace() {
-  var result = '';
-
-  if (typeof(arguments.caller) != 'undefined') { // IE, not ECMA
-    for (var a = arguments.caller; a != null; a = a.caller) {
-      result += '> ' + getFunctionName(a.callee) + '\n';
-      if (a.caller == a) {
-        result += '*';
-        break;
-      }
-    }
-  }
-  else { // Mozilla, not ECMA
-    // fake an exception so we can get Mozilla's error stack
-    var testExcp;
-    try
-    {
-      foo.bar;
-    }
-    catch(testExcp)
-    {
-      var stack = parseErrorStack(testExcp);
-      for (var i = 1; i < stack.length; i++)
-      {
-        result += '> ' + stack[i] + '\n';
-      }
-    }
-  }
-
-  return result;
-}
-
 function parseErrorStack(excp)
 {
   var stack = [];
@@ -303,10 +268,12 @@ function parseErrorStack(excp)
 function JsUnitException(comment, message) {
   this.isJsUnitException = true;
   this.comment           = comment;
-  this.jsUnitMessage     = message;
-  this.stackTrace        = getStackTrace();
+  this.message           = message;
+  this.stack             = (new Error()).stack;
 }
 
+JsUnitException.prototype = Object.create(Error.prototype, {});
+
 function warn() {
   if (top.tracer != null)
     top.tracer.warn(arguments[0], arguments[1]);
@@ -402,7 +369,6 @@ if (top && typeof(top.xbDEBUG) != 'undefined' && top.xbDEBUG.on && top.testManag
   top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'setUp');
   top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'tearDown');
   top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'getFunctionName');
-  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'getStackTrace');
   top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'warn');
   top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'inform');
   top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'debug');


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]