mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-07 13:41:21 +00:00
Handle error responses via JS.
This commit is contained in:
@@ -164,6 +164,33 @@ GuacamoleService.PermissionSet = function() {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the reponse from the given XMLHttpRequest object, throwing an error
|
||||||
|
* with a meaningful message if the request failed.
|
||||||
|
*
|
||||||
|
* @param {XMLHttpRequest} xhr The XMLHttpRequest to check the response of.
|
||||||
|
*/
|
||||||
|
GuacamoleService.handleResponse = function(xhr) {
|
||||||
|
|
||||||
|
// For HTTP Forbidden, just return permission denied
|
||||||
|
if (xhr.status == 403)
|
||||||
|
throw new Error("Permission denied.");
|
||||||
|
|
||||||
|
// Otherwise, if unsuccessful, throw error with message derived from
|
||||||
|
// response
|
||||||
|
if (xhr.status != 200) {
|
||||||
|
|
||||||
|
// Retrieve error message
|
||||||
|
var message = xhr.getResponseHeader("Guacamole-Error-Message")
|
||||||
|
|| xhr.statusText;
|
||||||
|
|
||||||
|
// Throw error with derived message
|
||||||
|
throw new Error(message);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collection of service functions which deal with connections. Each function
|
* Collection of service functions which deal with connections. Each function
|
||||||
* makes an explicit HTTP query to the server, and parses the response.
|
* makes an explicit HTTP query to the server, and parses the response.
|
||||||
@@ -198,9 +225,8 @@ GuacamoleService.Connections = {
|
|||||||
xhr.open("GET", list_url, false);
|
xhr.open("GET", list_url, false);
|
||||||
xhr.send(null);
|
xhr.send(null);
|
||||||
|
|
||||||
// If fail, throw error
|
// Handle response
|
||||||
if (xhr.status != 200)
|
GuacamoleService.handleResponse(xhr);
|
||||||
throw new Error(xhr.statusText);
|
|
||||||
|
|
||||||
// Otherwise, get list
|
// Otherwise, get list
|
||||||
var connections = new Array();
|
var connections = new Array();
|
||||||
@@ -289,9 +315,8 @@ GuacamoleService.Connections = {
|
|||||||
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||||
xhr.send(data);
|
xhr.send(data);
|
||||||
|
|
||||||
// If fail, throw error
|
// Handle response
|
||||||
if (xhr.status != 200)
|
GuacamoleService.handleResponse(xhr);
|
||||||
throw new Error(xhr.statusText);
|
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -323,9 +348,8 @@ GuacamoleService.Connections = {
|
|||||||
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||||
xhr.send(data);
|
xhr.send(data);
|
||||||
|
|
||||||
// If fail, throw error
|
// Handle response
|
||||||
if (xhr.status != 200)
|
GuacamoleService.handleResponse(xhr);
|
||||||
throw new Error(xhr.statusText);
|
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -348,9 +372,8 @@ GuacamoleService.Connections = {
|
|||||||
xhr.open("GET", connections_url, false);
|
xhr.open("GET", connections_url, false);
|
||||||
xhr.send(null);
|
xhr.send(null);
|
||||||
|
|
||||||
// If fail, throw error
|
// Handle response
|
||||||
if (xhr.status != 200)
|
GuacamoleService.handleResponse(xhr);
|
||||||
throw new Error(xhr.statusText);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -382,9 +405,8 @@ GuacamoleService.Users = {
|
|||||||
xhr.open("GET", users_url, false);
|
xhr.open("GET", users_url, false);
|
||||||
xhr.send(null);
|
xhr.send(null);
|
||||||
|
|
||||||
// If fail, throw error
|
// Handle response
|
||||||
if (xhr.status != 200)
|
GuacamoleService.handleResponse(xhr);
|
||||||
throw new Error(xhr.statusText);
|
|
||||||
|
|
||||||
// Otherwise, get list
|
// Otherwise, get list
|
||||||
var users = new Array();
|
var users = new Array();
|
||||||
@@ -477,9 +499,8 @@ GuacamoleService.Users = {
|
|||||||
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||||
xhr.send(data);
|
xhr.send(data);
|
||||||
|
|
||||||
// If fail, throw error
|
// Handle response
|
||||||
if (xhr.status != 200)
|
GuacamoleService.handleResponse(xhr);
|
||||||
throw new Error(xhr.statusText);
|
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -502,9 +523,8 @@ GuacamoleService.Users = {
|
|||||||
xhr.open("GET", users_url, false);
|
xhr.open("GET", users_url, false);
|
||||||
xhr.send(null);
|
xhr.send(null);
|
||||||
|
|
||||||
// If fail, throw error
|
// Handle response
|
||||||
if (xhr.status != 200)
|
GuacamoleService.handleResponse(xhr);
|
||||||
throw new Error(xhr.statusText);
|
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -527,9 +547,8 @@ GuacamoleService.Users = {
|
|||||||
xhr.open("GET", users_url, false);
|
xhr.open("GET", users_url, false);
|
||||||
xhr.send(null);
|
xhr.send(null);
|
||||||
|
|
||||||
// If fail, throw error
|
// Handle response
|
||||||
if (xhr.status != 200)
|
GuacamoleService.handleResponse(xhr);
|
||||||
throw new Error(xhr.statusText);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -573,9 +592,8 @@ GuacamoleService.Permissions = {
|
|||||||
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||||
xhr.send(data);
|
xhr.send(data);
|
||||||
|
|
||||||
// If fail, throw error
|
// Handle response
|
||||||
if (xhr.status != 200)
|
GuacamoleService.handleResponse(xhr);
|
||||||
throw new Error(xhr.statusText);
|
|
||||||
|
|
||||||
// Otherwise, build PermissionSet
|
// Otherwise, build PermissionSet
|
||||||
var i, type, name;
|
var i, type, name;
|
||||||
|
Reference in New Issue
Block a user