Hi Hackers,
PFA patch for proper error handling when connection will be lost to server.
I have created a common function alertify.pgRespErrorNotify in
web/pgadmin/static/js/alertify.pgadmin.defaults.js on the lines of existing
function alertify.pgNotifier. The function is called in error callback of
$.ajax call. The XHR is passed and handled in the function itself. Please
note that I have used JSON.parse and not $.parseJSON as it will be
deprecated in jQuery 3.x and thus removing many $.parseJSON calls.
Request you to kindly review.
Thanks and Regards,
Aditya Toshniwal
Software Engineer | EnterpriseDB Software Solutions | Pune
"Don't Complain about Heat, Plant a tree"
diff --git a/web/pgadmin/browser/server_groups/servers/databases/casts/static/js/cast.js b/web/pgadmin/browser/server_groups/servers/databases/casts/static/js/cast.js
index 97e689b..2e41ed6 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/casts/static/js/cast.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/casts/static/js/cast.js
@@ -209,15 +209,8 @@ define('pgadmin.node.cast', [
},
// On failure show error appropriate error message to user
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- alertify.error(err.errormsg);
- }
- } catch (e) {
- // Do nothing
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
},
});
}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/synonyms/static/js/synonym.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/synonyms/static/js/synonym.js
index e97dd05..d0ecaab 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/synonyms/static/js/synonym.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/synonyms/static/js/synonym.js
@@ -163,15 +163,8 @@ define('pgadmin.node.synonym', [
},
// On failure show error appropriate error message to user
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- alertify.error(err.errormsg);
- }
- } catch (e) {
- // Ignore
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
},
});
return res;
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js
index bf7ef55..857cf4c 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js
@@ -73,15 +73,8 @@ define('pgadmin.node.check_constraint', [
setTimeout(function() {t.select(i);}, 100);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.js
index 4997d17..29684b6 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.js
@@ -665,15 +665,8 @@ define('pgadmin.node.foreign_key', [
setTimeout(function() {t.select(i);}, 100);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/partitions/static/js/partition.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/partitions/static/js/partition.js
index fd53f74..c806d4b 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/partitions/static/js/partition.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/partitions/static/js/partition.js
@@ -174,15 +174,8 @@ function(
}, 10);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -233,15 +226,8 @@ function(
}, 10);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -283,15 +269,8 @@ function(
}, 10);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -335,15 +314,8 @@ function(
}
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
},
});
}
@@ -941,9 +913,8 @@ function(
);
}
},
- error: function(e) {
- var errmsg = $.parseJSON(e.responseText);
- Alertify.alert(gettext('Error fetching tables to be attached'), errmsg.errormsg);
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error, gettext('Error fetching tables to be attached'));
},
});
}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/static/js/table.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/static/js/table.js
index 647d1bf..9e60895 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/static/js/table.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/static/js/table.js
@@ -153,15 +153,8 @@ define('pgadmin.node.table', [
}, 10);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -212,15 +205,8 @@ define('pgadmin.node.table', [
}, 10);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -262,15 +248,8 @@ define('pgadmin.node.table', [
}, 10);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -302,15 +281,8 @@ define('pgadmin.node.table', [
t.select(i);
}, 10);
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -1047,9 +1019,8 @@ define('pgadmin.node.table', [
);
}
},
- error: function(e) {
- var errmsg = $.parseJSON(e.responseText);
- Alertify.alert(gettext('Error fetching tables to be attached'), errmsg.errormsg);
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
},
});
}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/__init__.py
index 5958474..07d389f 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/__init__.py
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/__init__.py
@@ -1015,6 +1015,7 @@ class TriggerView(PGChildNodeView):
# Convert str 'true' to boolean type
is_enable_flag = json.loads(data['enable'])
+ a=1/0
try:
SQL = render_template("/".join([self.template_path,
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/static/js/trigger.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/static/js/trigger.js
index fbe7659..c2cc3c5 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/static/js/trigger.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/static/js/trigger.js
@@ -130,15 +130,8 @@ define('pgadmin.node.trigger', [
}, 10);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -175,15 +168,8 @@ define('pgadmin.node.trigger', [
}, 10);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error, "Disable trigger failed");
t.unload(i);
},
});
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/views/static/js/mview.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/views/static/js/mview.js
index 795bbfc..6738c88 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/views/static/js/mview.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/views/static/js/mview.js
@@ -300,9 +300,8 @@ define('pgadmin.node.mview', [
);
}
},
- error: function(e) {
- var errmsg = $.parseJSON(e.responseText);
- Alertify.alert(gettext('Error refreshing view'), errmsg.errormsg);
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error, gettext('Error refreshing view'));
},
});
diff --git a/web/pgadmin/browser/server_groups/servers/databases/static/js/database.js b/web/pgadmin/browser/server_groups/servers/databases/static/js/database.js
index 4d5d514..e7c3fde 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/static/js/database.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/static/js/database.js
@@ -208,15 +208,8 @@ define('pgadmin.node.database', [
t.unload(i);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
diff --git a/web/pgadmin/browser/server_groups/servers/pgagent/static/js/pga_job.js b/web/pgadmin/browser/server_groups/servers/pgagent/static/js/pga_job.js
index 72fd1be..c2988b9 100644
--- a/web/pgadmin/browser/server_groups/servers/pgagent/static/js/pga_job.js
+++ b/web/pgadmin/browser/server_groups/servers/pgagent/static/js/pga_job.js
@@ -175,24 +175,8 @@ define('pgadmin.node.pga_job', [
// 'pgagent.pga_job' table updated with current time to run the job
// now.
success: function() { t.unload(i); },
- error: function(xhr) {
- var error_msg = gettext('Unable to run pgagent job.');
- if (xhr.readyState == 0) {
- alertify.error(
- gettext('Not connected to the server or the connection to the server has been closed.')
- );
- }
- else {
- if (_.isUndefined(xhr.responseText)) {
- alertify.error(error_msg);
- }
- else {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- alertify.error(err.errormsg);
- }
- }
- }
+ error: function(xhr, error, status) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
diff --git a/web/pgadmin/browser/server_groups/servers/static/js/server.js b/web/pgadmin/browser/server_groups/servers/static/js/server.js
index 375b0b6..d0a10e8 100644
--- a/web/pgadmin/browser/server_groups/servers/static/js/server.js
+++ b/web/pgadmin/browser/server_groups/servers/static/js/server.js
@@ -217,15 +217,8 @@ define('pgadmin.node.server', [
t.unload(i);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -299,15 +292,8 @@ define('pgadmin.node.server', [
Alertify.error(res.data.result);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -342,15 +328,8 @@ define('pgadmin.node.server', [
success: function(res) {
Alertify.success(res.data.result, 10);
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg, 10);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -520,15 +499,8 @@ define('pgadmin.node.server', [
Alertify.error(res.errormsg);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
},
});
}
@@ -547,15 +519,8 @@ define('pgadmin.node.server', [
}
Alertify.changeServerPassword(d).resizeTo('40%','52%');
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
},
});
@@ -590,15 +555,8 @@ define('pgadmin.node.server', [
}, 10);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
@@ -632,15 +590,8 @@ define('pgadmin.node.server', [
}, 10);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
t.unload(i);
},
});
diff --git a/web/pgadmin/browser/server_groups/servers/tablespaces/static/js/tablespace.js b/web/pgadmin/browser/server_groups/servers/tablespaces/static/js/tablespace.js
index 1dd02e6..e2cdbb9 100644
--- a/web/pgadmin/browser/server_groups/servers/tablespaces/static/js/tablespace.js
+++ b/web/pgadmin/browser/server_groups/servers/tablespaces/static/js/tablespace.js
@@ -285,15 +285,8 @@ define('pgadmin.node.tablespace', [
Alertify.error(res.errormsg);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
},
});
},
diff --git a/web/pgadmin/browser/static/js/browser.js b/web/pgadmin/browser/static/js/browser.js
index 897d270..4f37ff4 100644
--- a/web/pgadmin/browser/static/js/browser.js
+++ b/web/pgadmin/browser/static/js/browser.js
@@ -691,13 +691,8 @@ define('pgadmin.browser', [
modifyAnimation.modifyAcitreeAnimation(self);
modifyAnimation.modifyAlertifyAnimation(self);
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- Alertify.alert(gettext('Preference loading failed.'),
- err.errormsg
- );
- } catch (e) { console.warn(e.stack || e); }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
},
});
},
@@ -1826,13 +1821,8 @@ define('pgadmin.browser', [
}
fetchNodeInfo(_callback);
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) { console.warn(e.stack || e); }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
fetchNodeInfo(_callback);
},
});
diff --git a/web/pgadmin/dashboard/static/js/dashboard.js b/web/pgadmin/dashboard/static/js/dashboard.js
index e4f0dd5..9fa2a8d 100644
--- a/web/pgadmin/dashboard/static/js/dashboard.js
+++ b/web/pgadmin/dashboard/static/js/dashboard.js
@@ -97,15 +97,8 @@ define('pgadmin.dashboard', [
Alertify.error(txtError);
}
},
- error: function(xhr) {
- try {
- var err = $.parseJSON(xhr.responseText);
- if (err.success == 0) {
- Alertify.error(err.errormsg);
- }
- } catch (e) {
- console.warn(e.stack || e);
- }
+ error: function(xhr, status, error) {
+ alertify.pgRespErrorNotify(xhr, error);
},
});
},
diff --git a/web/pgadmin/static/js/alertify.pgadmin.defaults.js b/web/pgadmin/static/js/alertify.pgadmin.defaults.js
index cf2dd16..8dceac1 100644
--- a/web/pgadmin/static/js/alertify.pgadmin.defaults.js
+++ b/web/pgadmin/static/js/alertify.pgadmin.defaults.js
@@ -139,6 +139,46 @@ define([
).set('title', promptmsg).set('closable', true);
};
+ alertify.pgRespErrorNotify = (xhr, error, prefixMsg="") => {
+ var contentType = xhr.getResponseHeader('Content-Type')
+ try {
+ if (xhr.status === 0) {
+ error = gettext('Connection to the server has been lost.');
+ } else {
+ if(contentType){
+ if(contentType.indexOf('application/json') >= 0) {
+ var resp = JSON.parse(xhr.responseText);
+ error = _.escape(resp.result) || _.escape(resp.errormsg) || gettext('Unknown error');
+ }
+ }
+ if (contentType.indexOf('text/html') == 0) {
+ var alertMessage = '\
+ <div class="media font-red-3 text-14">\
+ <div class="media-body media-middle">\
+ <div class="alert-text">' + gettext('INTERNAL SERVER ERROR') + '</div><br/>\
+ <div class="alert-text">' + gettext('Click for details.') + '</div>\
+ </div>\
+ </div>';
+
+ alertify.notify(
+ alertMessage, "error", 0,
+ function() {
+ alertify.pgIframeDialog().show().set({
+ frameless: false,
+ }).set(
+ 'pg_msg', xhr.responseText
+ );
+ });
+ return;
+ }
+ }
+ }
+ catch(e){
+ error = e.message;
+ }
+ alertify.error(prefixMsg +" "+error);
+ }
+
var alertifyDialogResized = function(stop) {
var self = this;