1
0
mirror of https://github.com/dani/vroom.git synced 2024-06-29 15:03:41 +02:00

Consolidate api error handling in a function

This commit is contained in:
Daniel Berteaud 2015-02-05 21:46:02 +01:00
parent a8b75f01bd
commit 6deb8841af

View File

@ -41,6 +41,17 @@ $.ajaxSetup({
}
});
// Parse and display an error when an API call failed
function showApiError(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
}
// Handle lang switch
$('#switch_lang').change(function(){
$.ajax({
@ -53,13 +64,7 @@ $('#switch_lang').change(function(){
})
},
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg);
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
window.location.reload();
@ -308,13 +313,7 @@ $('#configureRoomForm').submit(function(e){
})
},
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
$('#ownerPass,#ownerPassConfirm,#joinPass,#joinPassConfirm').val('');
@ -342,9 +341,6 @@ function initIndex(){
}
else{
$.ajax({
url: rootUrl + 'api',
type: 'POST',
dataType: 'json',
data: {
req: JSON.stringify({
action: 'create_room',
@ -478,13 +474,7 @@ function initAdmin(){
})
},
error: function(data){
data = responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
roomList = data.rooms;
@ -507,7 +497,7 @@ function initAdmin(){
},
async: false,
error: function(data){
$.notify(locale.ERROR_OCCURRED, 'error');
showApiError(data);
},
success: function(data){
// Reset the list of email displayed, so first remove evry input field but the last one
@ -563,13 +553,7 @@ function initAdmin(){
})
},
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
$.notify(data.msg, 'success');
@ -652,13 +636,7 @@ function initVroom(room) {
},
async: false,
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
// Notify others if our role changed
@ -719,13 +697,7 @@ function initVroom(room) {
})
},
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
if (peers[id]){
@ -1081,13 +1053,7 @@ function initVroom(room) {
})
},
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
webrtc.sendToAll('owner_promoted', {peer: id});
@ -1185,13 +1151,7 @@ function initVroom(room) {
},
async: false,
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
if ($('#etherpadContainer').html() != ''){
@ -1499,13 +1459,7 @@ function initVroom(room) {
})
},
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
if (data.msg){
@ -1560,13 +1514,7 @@ function initVroom(room) {
})
},
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
if (data.msg){
@ -1672,13 +1620,7 @@ function initVroom(room) {
})
},
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
$('#recipient').val('');
@ -1868,13 +1810,7 @@ function initVroom(room) {
})
},
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
$('#authPass').val('');
@ -2050,13 +1986,7 @@ function initVroom(room) {
},
async: false,
error: function(data){
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data){
if (data.msg && data.msg != ''){
@ -2071,7 +2001,7 @@ function initVroom(room) {
if (etherpad.enabled){
$('#etherpadButton').change(function(){
var action = ($(this).is(':checked')) ? 'show':'hide';
if (action == 'show'){
if (action === 'show'){
// If not already loaded, load etherpad in the iFrame
if ($('#etherpadContainer').html() == ''){
loadEtherpadIframe();
@ -2099,13 +2029,7 @@ function initVroom(room) {
})
},
error: function(data) {
data = data.responseJSON;
if (data.msg){
$.notify(data.msg, 'error');
}
else{
$.notify(locale.ERROR_OCCURRED, 'error');
}
showApiError(data);
},
success: function(data) {
if (data.msg && data.msg != ''){