Skip to content

Commit

Permalink
properly stop on failure to execute a request in sequence
Browse files Browse the repository at this point in the history
we used to accept requests that have actually made it to ES. Now we only continue on 2xx or 404. Improved error formatting too.
  • Loading branch information
bleskes committed Sep 18, 2015
1 parent ce87621 commit a634be9
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 133 deletions.
131 changes: 0 additions & 131 deletions public/webpackShims/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ define([
$(document.body).removeClass('fouc');

var $esServer = miscInputs.$esServer;
var $send = miscInputs.$send;

var marvelOpts = localStorage.getItem('marvelOpts');
if (marvelOpts) {
Expand All @@ -55,121 +54,6 @@ define([
marvelOpts = {status: 'trial'};
}

var CURRENT_REQ_ID = 0;

function submitCurrentRequestToES() {

var req_id = ++CURRENT_REQ_ID;

input.getRequestsInRange(function (requests) {
if (req_id != CURRENT_REQ_ID) {
return;
}
output.update('');

if (requests.length == 0) {
return;
}

var isMultiRequest = requests.length > 1;

saveCurrentState();

$("#notification").text("Calling ES....").css("visibility", "visible");

var finishChain = function () {
$("#notification").text("").css("visibility", "hidden");
};

var isFirstRequest = true;

var sendNextRequest = function () {
if (req_id != CURRENT_REQ_ID) {
return;
}
if (requests.length == 0) {
finishChain();
return;
}
var req = requests.shift();
var es_path = req.url;
var es_method = req.method;
var es_data = req.data.join("\n");
if (es_data) {
es_data += "\n";
} //append a new line for bulk requests.

es.send(es_method, es_path, es_data).always(function (dataOrjqXHR, textStatus, jqXhrORerrorThrown) {
if (req_id != CURRENT_REQ_ID) {
return;
}
var xhr;
if (dataOrjqXHR.promise) {
xhr = dataOrjqXHR;
}
else {
xhr = jqXhrORerrorThrown;
}
if (typeof xhr.status == "number" &&
((xhr.status >= 400 && xhr.status < 600) ||
(xhr.status >= 200 && xhr.status < 300)
)) {
// we have someone on the other side. Add to history
history.addToHistory(es.getBaseUrl(), es_path, es_method, es_data);


var value = xhr.responseText;
var mode = null;
var contentType = xhr.getAllResponseHeaders("Content-Type") || "";
if (contentType.indexOf("text/plain") >= 0) {
mode = "ace/mode/text";
}
else if (contentType.indexOf("application/yaml") >= 0) {
mode = "ace/mode/yaml"
}
else {
// assume json - auto pretty
try {
value = JSON.stringify(JSON.parse(value), null, 3);
}
catch (e) {

}
}

if (isMultiRequest) {
value = "# " + req.method + " " + req.url + "\n" + value;
}
if (isFirstRequest) {
output.update(value, mode);
}
else {
output.append("\n" + value);
}
isFirstRequest = false;
// single request terminate via sendNextRequest as well
sendNextRequest();
}
else {
var value = "Request failed to get to the server (status code: " + xhr.status + "):" + xhr.responseText;
if (isMultiRequest) {
value = "# " + req.method + " " + req.url + "\n" + value;
}
if (isFirstRequest) {
output.update(value, 'ace/mode/text');
}
else {
output.append("\n" + value);
}
finishChain();
}
});
};

sendNextRequest();
});
}

// set the value of the server and/or the input and clear the output
function resetToValues(server, content) {
if (server != null) {
Expand Down Expand Up @@ -335,21 +219,6 @@ define([

}());

/**
* Setup the "send" shortcut
*/
input.commands.addCommand({
name: 'send to elasticsearch',
bindKey: {win: 'Ctrl-Enter', mac: 'Command-Enter'},
exec: submitCurrentRequestToES
});

$send.click(function () {
input.focus();
submitCurrentRequestToES();
return false;
});

/**
* Display the welcome popup if it has not been shown yet
*/
Expand Down
150 changes: 149 additions & 1 deletion public/webpackShims/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ define([
'settings',
'require',
'utils',
'es',
'history',
'vendor/zeroclip',
'ace/ext-searchbox'
], function (ace, Autocomplete, $, mappings, output, SenseEditor, settings, require, utils, ZeroClipboard) {
], function (ace, Autocomplete, $, mappings, output, SenseEditor, settings, require, utils, es, history,
ZeroClipboard) {
'use strict';

var input = new SenseEditor($('#editor'));
Expand Down Expand Up @@ -107,6 +110,149 @@ define([
return zc;
}());

/**
* Setup the "send" shortcut
*/

var CURRENT_REQ_ID = 0;

function sendCurrentRequestToES() {

var req_id = ++CURRENT_REQ_ID;

input.getRequestsInRange(function (requests) {
if (req_id != CURRENT_REQ_ID) {
return;
}
output.update('');

if (requests.length == 0) {
return;
}

var isMultiRequest = requests.length > 1;

$("#notification").text("Calling ES....").css("visibility", "visible");

var finishChain = function () {
$("#notification").text("").css("visibility", "hidden");
};

var isFirstRequest = true;

var sendNextRequest = function () {
if (req_id != CURRENT_REQ_ID) {
return;
}
if (requests.length == 0) {
finishChain();
return;
}
var req = requests.shift();
var es_path = req.url;
var es_method = req.method;
var es_data = req.data.join("\n");
if (es_data) {
es_data += "\n";
} //append a new line for bulk requests.

es.send(es_method, es_path, es_data).always(function (dataOrjqXHR, textStatus, jqXhrORerrorThrown) {
if (req_id != CURRENT_REQ_ID) {
return;
}
var xhr;
if (dataOrjqXHR.promise) {
xhr = dataOrjqXHR;
}
else {
xhr = jqXhrORerrorThrown;
}
function modeForContentType(contentType) {
if (contentType.indexOf("text/plain") >= 0) {
return "ace/mode/text";
}
else if (contentType.indexOf("application/yaml") >= 0) {
return "ace/mode/yaml";
}
return null;
}

if (typeof xhr.status == "number" &&
// things like DELETE index where the index is not there are OK.
((xhr.status >= 200 && xhr.status < 300) || xhr.status == 404)
) {
// we have someone on the other side. Add to history
history.addToHistory(es.getBaseUrl(), es_path, es_method, es_data);


let value = xhr.responseText;
let mode = modeForContentType(xhr.getAllResponseHeaders("Content-Type") || "");

if (mode === null || mode === "application/json") {
// assume json - auto pretty
try {
value = JSON.stringify(JSON.parse(value), null, 3);
}
catch (e) {

}
}

if (isMultiRequest) {
value = "# " + req.method + " " + req.url + "\n" + value;
}
if (isFirstRequest) {
output.update(value, mode);
}
else {
output.append("\n" + value);
}
isFirstRequest = false;
// single request terminate via sendNextRequest as well
sendNextRequest();
}
else {
let value, mode;
if (xhr.responseText) {
value = xhr.responseText; // ES error should be shown
mode = modeForContentType(xhr.getAllResponseHeaders("Content-Type") || "");
if (value[0] == "{") {
try {
value = JSON.stringify(JSON.parse(value), null, 3);
}
catch (e) {
}
}
} else {
value = "Request failed to get to the server (status code: " + xhr.status + ")";
mode = 'ace/mode/text';
}
if (isMultiRequest) {
value = "# " + req.method + " " + req.url + "\n" + value;
}
if (isFirstRequest) {
output.update(value, mimetype);
}
else {
output.append("\n" + value);
}
finishChain();
}
});
};

sendNextRequest();
});
}


input.commands.addCommand({
name: 'send to elasticsearch',
bindKey: {win: 'Ctrl-Enter', mac: 'Command-Enter'},
exec: sendCurrentRequestToES
});


/**
* Init the editor
*/
Expand All @@ -116,5 +262,7 @@ define([
input.focus();
input.highlightCurrentRequestsAndUpdateActionBar();

input.sendCurrentRequestToES = sendCurrentRequestToES;

return input;
});
7 changes: 6 additions & 1 deletion public/webpackShims/misc_inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ define([
$esServer.val(server);
});

$send.click(function () {
input.focus();
input.sendCurrentRequestToES();
return false;
});

return {
$esServer: $esServer,
$send: $send,
$autoIndent: $autoIndent,
$header: $header,
$resizer: $resizer
Expand Down

0 comments on commit a634be9

Please sign in to comment.