THRIFT-1232 JavaScript TException should be a constructor function
Patch: Pascal Bach
file: 1232-Improve-Exception-handling-and-make-error-hierarchy.patch

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1144292 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_js_generator.cc b/compiler/cpp/src/generate/t_js_generator.cc
index 7561b12..b6ca05a 100644
--- a/compiler/cpp/src/generate/t_js_generator.cc
+++ b/compiler/cpp/src/generate/t_js_generator.cc
@@ -576,6 +576,7 @@
     out << "Thrift.inherits(" <<
         js_namespace(tstruct->get_program()) <<
         tstruct->get_name() << ", Thrift.TException);" << endl;
+	out << js_namespace(tstruct->get_program())<<tstruct->get_name() <<".prototype.name = '" << tstruct->get_name() << "';" << endl;
   } else {
     //init prototype
     out << js_namespace(tstruct->get_program())<<tstruct->get_name() <<".prototype = {};\n";
diff --git a/lib/js/thrift.js b/lib/js/thrift.js
index 8b1733e..860cb99 100644
--- a/lib/js/thrift.js
+++ b/lib/js/thrift.js
@@ -72,14 +72,12 @@
 };
 
 
-Thrift.TException = {};
-Thrift.TException.prototype = {
-    initialize: function(message, code) {
-        this.message = message;
-        this.code = (code === null) ? 0 : code;
-    }
-};
 
+Thrift.TException = function(message) {
+    this.message = message;
+};
+Thrift.inherits(Thrift.TException, Error);
+Thrift.TException.prototype.name = 'TException';
 
 Thrift.TApplicationExceptionType = {
     'UNKNOWN' : 0,
@@ -94,80 +92,75 @@
     this.message = message;
     this.code = (code === null) ? 0 : code;
 };
+Thrift.inherits(Thrift.TApplicationException, Thrift.TException);
+Thrift.TApplicationException.prototype.name = 'TApplicationException';
 
-Thrift.TApplicationException.prototype = {
+Thrift.TApplicationException.prototype.read = function(input) {
+    while (1) {
+        var ret = input.readFieldBegin();
 
-    read: function(input) {
-        while (1) {
-            var ret = input.readFieldBegin();
+        if (ret.ftype == Thrift.Type.STOP) {
+            break;
+        }
 
-            if (ret.ftype == Thrift.Type.STOP) {
-                break;
-            }
+        var fid = ret.fid;
 
-            var fid = ret.fid;
-
-            switch (fid) {
-                case 1:
-                    if (ret.ftype == Thrift.Type.STRING) {
-                        ret = input.readString();
-                        this.message = ret.value;
-                    } else {
-                        ret = input.skip(ret.ftype);
-                    }
-
-                    break;
-                case 2:
-                    if (ret.ftype == Thrift.Type.I32) {
-                        ret = input.readI32();
-                        this.code = ret.value;
-                    } else {
-                        ret = input.skip(ret.ftype);
-                    }
-                    break;
-
-                default:
+        switch (fid) {
+            case 1:
+                if (ret.ftype == Thrift.Type.STRING) {
+                    ret = input.readString();
+                    this.message = ret.value;
+                } else {
                     ret = input.skip(ret.ftype);
-                    break;
-            }
-
-            input.readFieldEnd();
+                }
+                break;
+            case 2:
+                if (ret.ftype == Thrift.Type.I32) {
+                    ret = input.readI32();
+                    this.code = ret.value;
+                } else {
+                    ret = input.skip(ret.ftype);
+                }
+                break;
+           default:
+                ret = input.skip(ret.ftype);
+                break;
         }
 
-        input.readStructEnd();
-    },
-
-    write: function(output) {
-        var xfer = 0;
-
-        output.writeStructBegin('TApplicationException');
-
-        if (this.message) {
-            output.writeFieldBegin('message', Thrift.Type.STRING, 1);
-            output.writeString(this.getMessage());
-            output.writeFieldEnd();
-        }
-
-        if (this.code) {
-            output.writeFieldBegin('type', Thrift.Type.I32, 2);
-            output.writeI32(this.code);
-            output.writeFieldEnd();
-        }
-
-        output.writeFieldStop();
-        output.writeStructEnd();
-    },
-
-    getCode: function() {
-        return this.code;
-    },
-
-    getMessage: function() {
-        return this.message;
+        input.readFieldEnd();
     }
+
+    input.readStructEnd();
 };
 
+Thrift.TApplicationException.prototype.write = function(output) {
+    var xfer = 0;
 
+    output.writeStructBegin('TApplicationException');
+
+    if (this.message) {
+        output.writeFieldBegin('message', Thrift.Type.STRING, 1);
+        output.writeString(this.getMessage());
+        output.writeFieldEnd();
+    }
+
+    if (this.code) {
+        output.writeFieldBegin('type', Thrift.Type.I32, 2);
+        output.writeI32(this.code);
+        output.writeFieldEnd();
+    }
+
+    output.writeFieldStop();
+    output.writeStructEnd();
+};
+
+Thrift.TApplicationException.prototype.getCode = function() {
+    return this.code;
+};
+
+Thrift.TApplicationException.prototype.getMessage = function() {
+    return this.message;
+};
 
 /**
  *If you do not specify a url then you must handle ajax on your own.