Change "Connection does not exist" to more user-friendly error.
[guacamole-common-js.git] / src / main / resources / tunnel.js
index a35d007..87155eb 100644 (file)
@@ -203,13 +203,40 @@ Guacamole.HTTPTunnel = function(tunnelURL) {
 
     }
 
+    function getHTTPTunnelErrorMessage(xmlhttprequest) {
+
+        var status = xmlhttprequest.status;
+
+        // Special cases
+        if (status == 0)   return "Disconnected";
+        if (status == 200) return "Success";
+        if (status == 403) return "Unauthorized";
+        if (status == 404) return "Connection closed"; /* While it may be more
+                                                        * accurate to say the
+                                                        * connection does not
+                                                        * exist, it is confusing
+                                                        * to the user.
+                                                        * 
+                                                        * In general, this error
+                                                        * will only happen when
+                                                        * the tunnel does not
+                                                        * exist, which happens
+                                                        * after the connection
+                                                        * is closed and the
+                                                        * tunnel is detached.
+                                                        */
+        // Internal server errors
+        if (status >= 500 && status <= 599) return "Server error";
+
+        // Otherwise, unknown
+        return "Unknown error";
+
+    }
 
     function handleHTTPTunnelError(xmlhttprequest) {
 
-        // Get error message (if any)
-        var message = xmlhttprequest.getResponseHeader("X-Guacamole-Error-Message");
-        if (!message)
-            message = "Internal server error";
+        // Get error message
+        var message = getHTTPTunnelErrorMessage(xmlhttprequest);
 
         // Call error handler
         if (tunnel.onerror) tunnel.onerror(message);
@@ -424,13 +451,8 @@ Guacamole.HTTPTunnel = function(tunnelURL) {
 
         // If failure, throw error
         if (connect_xmlhttprequest.status != 200) {
-
-            var message = connect_xmlhttprequest.getResponseHeader("X-Guacamole-Error-Message");
-            if (!message)
-                message = "Internal error";
-
+            var message = getHTTPTunnelErrorMessage(connect_xmlhttprequest);
             throw new Error(message);
-
         }
 
         // Get UUID from response