mirror of
https://github.com/prymitive/karma
synced 2026-05-05 03:16:51 +00:00
Merge pull request #138 from cloudflare/ajax-errors
Add a helper to converting ajax() call errors into a string with desc…
This commit is contained in:
@@ -147,22 +147,8 @@ var Silence = (function() {
|
||||
type: "POST",
|
||||
url: alertmanagerSilencesAPIUrl(url),
|
||||
data: JSON.stringify(payload),
|
||||
error: function(xhr, textStatus, errorThrown) {
|
||||
// default to whatever error text we can get
|
||||
var err = xhr.responseText || errorThrown || textStatus;
|
||||
if (xhr.responseText) {
|
||||
// if we have a reponse text try to decode it as JSON
|
||||
// it should be error from Alertmanager (it we were able to connect)
|
||||
try {
|
||||
var j = JSON.parse(xhr.responseText);
|
||||
if (j.error !== undefined) {
|
||||
err = j.error;
|
||||
}
|
||||
} catch (error) {
|
||||
// can't parse json, do nothing
|
||||
}
|
||||
}
|
||||
|
||||
error: function(xhr, textStatus) {
|
||||
var err = Unsee.ParseAJAXError(xhr, textStatus);
|
||||
var errContent = Templates.Render("silenceFormError", {error: err});
|
||||
$(elem).html(errContent);
|
||||
},
|
||||
@@ -198,8 +184,8 @@ var Silence = (function() {
|
||||
});
|
||||
$.ajax({
|
||||
url: "alerts.json?q=alertname=" + elem.data("alertname"),
|
||||
error: function(xhr, textStatus, errorThrown) {
|
||||
var err = xhr.responseText || errorThrown || textStatus;
|
||||
error: function(xhr, textStatus) {
|
||||
var err = Unsee.ParseAJAXError(xhr, textStatus);
|
||||
modal.find(".modal-body").html(
|
||||
Templates.Render("silenceFormFatal", {error: err})
|
||||
);
|
||||
|
||||
@@ -47,6 +47,22 @@ var Unsee = (function() {
|
||||
}
|
||||
};
|
||||
|
||||
var parseAJAXError = function(xhr, textStatus) {
|
||||
// default to textStatus, it's usually just "error" string
|
||||
var err = textStatus;
|
||||
if (xhr.readyState === 0) {
|
||||
// ajax() completed but request wasn't send
|
||||
err = "Connection to the remote endpoint failed";
|
||||
} else if (xhr.responseJSON && xhr.responseJSON.error) {
|
||||
// there's response JSON and an error key in it
|
||||
err = xhr.responseJSON.error;
|
||||
} else if (xhr.responseText) {
|
||||
// else check response as a string
|
||||
err = xhr.responseText;
|
||||
}
|
||||
return err;
|
||||
};
|
||||
|
||||
var init = function() {
|
||||
Progress.Init();
|
||||
|
||||
@@ -233,15 +249,16 @@ var Unsee = (function() {
|
||||
}
|
||||
}
|
||||
},
|
||||
error: function() {
|
||||
error: function(xhr, textStatus) {
|
||||
Counter.Unknown();
|
||||
$(selectors.instanceErrors).html("");
|
||||
// if fatal error was already triggered we have error message
|
||||
// so don't add new one
|
||||
if (!Watchdog.IsFatal()) {
|
||||
var err = Unsee.ParseAJAXError(xhr, textStatus);
|
||||
renderError("updateError", {
|
||||
error: "Backend error",
|
||||
messages: [ "AJAX request failed" ],
|
||||
messages: [ err ],
|
||||
lastTs: Watchdog.GetLastUpdate()
|
||||
});
|
||||
}
|
||||
@@ -291,7 +308,8 @@ var Unsee = (function() {
|
||||
Reload: triggerReload,
|
||||
GetRefreshRate: getRefreshRate,
|
||||
SetRefreshRate: setRefreshRate,
|
||||
Flash: flash
|
||||
Flash: flash,
|
||||
ParseAJAXError: parseAJAXError
|
||||
};
|
||||
|
||||
})();
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user