Cleanup error handling in ajax requests

master
Daniel Berteaud 11 years ago
parent fdff058521
commit 2800c35dca
  1. 31
      public/js/vroom.js
  2. 14
      public/vroom.pl

@ -528,12 +528,16 @@ function initVroom(room) {
room: roomName room: roomName
}, },
error: function(data) { error: function(data) {
var msg = (data && data.msg) ? data.msg : locale.ERROR_OCCURED; $.notify(locale.ERROR_OCCURED, 'error');
$.notify(msg, 'error');
}, },
success: function(data) { success: function(data) {
$.notify(data.msg, 'success');
$('#recipient,#message').val(''); $('#recipient,#message').val('');
if (data.status == 'success'){
$.notify(data.msg, 'success');
}
else{
$.notify(data.msg, 'error');
}
} }
}); });
}); });
@ -653,8 +657,7 @@ function initVroom(room) {
room: roomName room: roomName
}, },
error: function(data) { error: function(data) {
var msg = locale.ERROR_OCCURED; $.notify(locale.ERROR_OCCURED, 'error');
$.notify(msg, 'error');
}, },
success: function(data) { success: function(data) {
$('#authPass').val(''); $('#authPass').val('');
@ -694,8 +697,7 @@ function initVroom(room) {
room: roomName room: roomName
}, },
error: function() { error: function() {
var msg = locale.ERROR_OCCURED; $.notify(locale.ERROR_OCCURED, 'error');
$.notify(msg, 'error');
}, },
success: function(data) { success: function(data) {
$('#joinPass').val(''); $('#joinPass').val('');
@ -719,8 +721,7 @@ function initVroom(room) {
room: roomName room: roomName
}, },
error: function() { error: function() {
var msg = locale.ERROR_OCCURED; $.notify(locale.ERROR_OCCURED, 'error');
$.notify(msg, 'error');
}, },
success: function(data) { success: function(data) {
$('#joinPass').val(''); $('#joinPass').val('');
@ -757,8 +758,7 @@ function initVroom(room) {
room: roomName room: roomName
}, },
error: function() { error: function() {
var msg = locale.ERROR_OCCURED; $.notify(locale.ERROR_OCCURED, 'error');
$.notify(msg, 'error');
}, },
success: function(data) { success: function(data) {
$('#ownerPass').val(''); $('#ownerPass').val('');
@ -782,8 +782,7 @@ function initVroom(room) {
room: roomName room: roomName
}, },
error: function() { error: function() {
var msg = locale.ERROR_OCCURED; $.notify(locale.ERROR_OCCURED, 'error');
$.notify(msg, 'error');
}, },
success: function(data) { success: function(data) {
$('#ownerPass').val(''); $('#ownerPass').val('');
@ -901,12 +900,10 @@ function initVroom(room) {
room: roomName room: roomName
}, },
error: function(data) { error: function(data) {
var msg = (data && data.msg) ? data.msg : locale.ERROR_OCCURED; $.notify(locale.ERROR_OCCURED, 'error');
$.notify(msg, 'error');
}, },
success: function(data) { success: function(data) {
// In case of success, only notify if the server replied something if (data.status == 'success' && data.msg && data.msg != ''){
if (data.msg !== ''){
$.notify(data.msg, 'success'); $.notify(data.msg, 'success');
} }
} }

@ -456,17 +456,17 @@ post '/action' => sub {
return $self->render( return $self->render(
json => { json => {
msg => $self->l('ERROR_NOT_LOGGED_IN'), msg => $self->l('ERROR_NOT_LOGGED_IN'),
status => 'error'
}, },
status => 403
); );
} }
$self->stash(room => $room); $self->stash(room => $room);
my $data = $self->get_room($room); my $data = $self->get_room($room);
return $self->render( return $self->render(
json => { json => {
msg => sprintf ($self->l("ERROR_ROOM_s_DOESNT_EXIST"), $room) msg => sprintf ($self->l("ERROR_ROOM_s_DOESNT_EXIST"), $room),
status => 'error'
}, },
status => '500'
) unless ($data); ) unless ($data);
if ($action eq 'invite'){ if ($action eq 'invite'){
@ -486,13 +486,14 @@ post '/action' => sub {
return $self->render( return $self->render(
json => { json => {
msg => $self->l('ERROR_OCCURED'), msg => $self->l('ERROR_OCCURED'),
status => 'error'
}, },
status => 500
); );
$self->app->log->info($self->session('name') . " sent an invitation for room $room to $rcpt"); $self->app->log->info($self->session('name') . " sent an invitation for room $room to $rcpt");
$self->render( $self->render(
json => { json => {
msg => sprintf($self->l('INVITE_SENT_TO_s'), $rcpt) msg => sprintf($self->l('INVITE_SENT_TO_s'), $rcpt),
status => 'success'
} }
); );
} }
@ -506,14 +507,15 @@ post '/action' => sub {
return $self->render( return $self->render(
json => { json => {
msg => $self->l('ERROR_OCCURED'), msg => $self->l('ERROR_OCCURED'),
status => 'error'
}, },
status => '500'
); );
} }
else{ else{
return $self->render( return $self->render(
json => { json => {
msg => '', msg => '',
status => 'success'
} }
); );
} }

Loading…
Cancel
Save