Skip to content

Commit

Permalink
Show an error message when a response does not have a JSON text
Browse files Browse the repository at this point in the history
  • Loading branch information
sh19910711 committed Aug 12, 2015
1 parent b561e8a commit d8a373c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
12 changes: 11 additions & 1 deletion extensions/chrome/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,21 @@ function initPanelMessage() {
function initReqRes() {
chrome.runtime.onMessage.addListener(handleMessage);

function extractProps(xhr) {
var props = {};
for (var key in xhr) {
if (typeof xhr[key] === 'string' || typeof xhr[key] === 'number') {
props[key] = xhr[key];
}
}
return props;
}

function handleMessage(req, sender, sendResponse) {
if (req.type === 'request') {
var url = tabInfo[req.tabId].remoteHost + '/' + req.url;
REPLConsole.request(req.method, url, req.params, function(xhr) {
sendResponse({ status: xhr.status, responseText: xhr.responseText });
sendResponse(extractProps(xhr));
});
}
return true;
Expand Down
25 changes: 23 additions & 2 deletions lib/web_console/templates/console.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,34 @@ REPLConsole.prototype.commandHandle = function(line, callback) {
return status >= 200 && status < 300 || status === 304;
}

function parseJSON(text) {
try {
return JSON.parse(text);
} catch (e) {
return null;
}
}

function getErrorText(xhr) {
if (!xhr.status) {
return "Oops! Failed to connect to Web Console middleware.\n" +
"Please make sure that web server is running.";
} else {
return xhr.status + ' ' + xhr.statusText;
}
}

putRequest(self.getUrl(), params, function(xhr) {
var response = JSON.parse(xhr.responseText);
var response = parseJSON(xhr.responseText);
var result = isSuccess(xhr.status);
if (result) {
self.writeOutput(response.output);
} else {
self.writeError(response.output);
if (response && response.output) {
self.writeError(response.output);
} else {
self.writeError(getErrorText(xhr));
}
}
callback(result, response);
});
Expand Down

0 comments on commit d8a373c

Please sign in to comment.