Skip to content

Commit

Permalink
better error messsage display
Browse files Browse the repository at this point in the history
  • Loading branch information
gildas-lormeau committed Nov 26, 2011
1 parent e9775f8 commit ef88570
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 55 deletions.
13 changes: 8 additions & 5 deletions WebContent/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@ function init() {
var workerFormatter, workerJSONLint, json = msg.json;

function onWorkerJSONLintMessage() {
var message = JSON.parse(event.data);
workerJSONLint.removeEventListener("message", onWorkerJSONLintMessage, false);
workerJSONLint.terminate();
port.postMessage({
ongetError : true,
error : event.data
error : message.error,
loc : message.loc,
offset : msg.offset
});
}

function onWorkerFormatterMessage(event) {
var msg = event.data;
var message = event.data;
workerFormatter.removeEventListener("message", onWorkerFormatterMessage, false);
workerFormatter.terminate();
if (msg.html)
if (message.html)
port.postMessage({
onjsonToHTML : true,
html : msg.html,
html : message.html,
theme : localStorage.theme
});
if (msg.error) {
if (message.error) {
workerJSONLint = new Worker("workerJSONLint.js");
workerJSONLint.addEventListener("message", onWorkerJSONLintMessage, false);
workerJSONLint.postMessage(json);
Expand Down
94 changes: 62 additions & 32 deletions WebContent/content.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,76 @@
var port = chrome.extension.connect(), collapsers;

function displayError(error) {
document.body.innerHTML += '<link rel="stylesheet" type="text/css" href="' + chrome.extension.getURL("content_error.css") + '">';
errorBox = document.createElement("pre");
closeBox = document.createElement("div");
errorBox.className = "error";
closeBox.className = "close-error";
closeBox.onclick = function() {
errorBox.parentElement.removeChild(errorBox);
function displayError(error, loc, offset) {
var link = document.createElement("link"), pre = document.body.firstChild.firstChild, text = pre.textContent.substring(offset), start = 0, ranges = [], idx = 0, end, range = document
.createRange(), imgError = document.createElement("img"), content = document.createElement("div"), errorPosition = document.createElement("span"), container = document
.createElement("div"), closeButton = document.createElement("div");
link.rel = "stylesheet";
link.type = "text/css";
link.href = chrome.extension.getURL("content_error.css");
document.head.appendChild(link);
while (idx != -1) {
idx = text.indexOf("\n", start);
ranges.push(start);
start = idx + 1;
}
start = ranges[loc.first_line - 1] + loc.first_column + offset;
end = ranges[loc.last_line - 1] + loc.last_column + offset;
range.setStart(pre, start);
if (start == end -1)
range.setEnd(pre, start);
else
range.setEnd(pre, end);
errorPosition.className = "error-position";
errorPosition.id = "error-position";
range.surroundContents(errorPosition);
imgError.src = "error.gif";
errorPosition.insertBefore(imgError, errorPosition.firstChild);
content.className = "content";
closeButton.className = "close-error";
closeButton.onclick = function() {
content.parentElement.removeChild(content);
};
errorBox.textContent = error;
errorBox.appendChild(closeBox);
setTimeout(function() {
document.body.appendChild(errorBox);
errorBox.style.pixelLeft = Math.max(0, Math.floor((window.innerWidth - errorBox.offsetWidth) / 2));
errorBox.style.pixelTop = Math.max(0, Math.floor((window.innerHeight - errorBox.offsetHeight) / 2));
}, 100);
content.textContent = error;
content.appendChild(closeButton);
container.className = "container";
container.appendChild(content);
errorPosition.parentNode.insertBefore(container, errorPosition.nextSibling);
location.hash = "error-position";
history.replaceState({}, "", "#");
}

function displayObject(json, fnName) {
function displayObject(json, fnName, offset) {
if (!json)
return;
port.postMessage({
jsonToHTML : true,
json : json,
fnName : fnName
fnName : fnName,
offset : offset
});
}

function extractData(text) {
var tokens;
if ((text.charAt(0) == "{" || text.charAt(0) == "[") && (text.charAt(text.length - 1) == "}" || text.charAt(text.length - 1) == "]"))
return {
text : text
};
tokens = text.match(/^([^\s\(]*)\s*\(\s*([\[{].*[\]}])\s*\)(?:\s*;?)*\s*$/);
if (tokens && tokens[1] && tokens[2])
function extractData(rawText) {
var tokens, text = rawText.trim();

function test(text) {
return ((text.charAt(0) == "[" && text.charAt(text.length - 1) == "]") || (text.charAt(0) == "{" && text.charAt(text.length - 1) == "}"));
}

if (test(text))
return {
fnName : tokens[1],
text : tokens[2]
text : rawText,
offset : 0
};
tokens = text.match(/^([^\s\(]*)\s*\(([\s\S]*)\)\s*;?$/);
if (tokens && tokens[1] && tokens[2]) {
if (test(tokens[2].trim()))
return {
fnName : tokens[1],
text : tokens[2],
offset : rawText.indexOf(tokens[2])
};
}
}

function processData(data, options) {
Expand All @@ -50,13 +81,13 @@ function processData(data, options) {
if (this.readyState == 4) {
data = extractData(this.responseText);
if (data)
displayObject(data.text, data.fnName);
displayObject(data.text, data.fnName, data.offset);
}
};
xhr.open("GET", document.location.href, true);
xhr.send(null);
} else if (data)
displayObject(data.text, data.fnName);
displayObject(data.text, data.fnName, data.offset);
}

function ontoggle(event) {
Expand Down Expand Up @@ -134,7 +165,6 @@ function init(data) {
if (msg.onjsonToHTML)
if (msg.html) {
content += '<link rel="stylesheet" type="text/css" href="' + chrome.extension.getURL("jsonview-core.css") + '">';
// content += '<link rel="stylesheet" type="text/css" href="' + chrome.extension.getURL("jsonview.css") + '">';
content += "<style>" + msg.theme + "</style>";
content += msg.html;
document.body.innerHTML = content;
Expand Down Expand Up @@ -176,7 +206,7 @@ function init(data) {
fnName : fnName
});
if (msg.ongetError) {
displayError(msg.error);
displayError(msg.error, msg.loc, msg.offset);
}
});
port.postMessage({
Expand All @@ -188,7 +218,7 @@ function load() {
var child, data;
if (document.body && (document.body.childNodes[0] && document.body.childNodes[0].tagName == "PRE" || document.body.children.length == 0)) {
child = document.body.children.length ? document.body.childNodes[0] : document.body;
data = extractData(child.innerText.trim());
data = extractData(child.innerText);
if (data)
init(data);
}
Expand Down
29 changes: 25 additions & 4 deletions WebContent/content_error.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
.error {
.error-position {
display: inline-block;
color: fireBrick;
padding-left: 3px;
padding-right: 3px;
}

.error-position img {
padding-right: 3px;
height: .8em;
}

.container {
position: absolute;
left: 0px;
width: 100%;
}

.content {
top: 3px;
width: 30em;
margin: 0 auto;
background-color: #FEE;
border: 2px solid #933;
color: #933;
padding: 20px;
display: inline-block;
position: fixed;
position: relative;
-webkit-box-shadow: #888 3px 3px 5px;
}

.close-error {
Expand All @@ -15,4 +36,4 @@
top: 3px;
right: 3px;
cursor: pointer;
}
}
Binary file added WebContent/error.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 14 additions & 14 deletions WebContent/workerJSONLint.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ next:function () {
this.yylloc = {first_line: this.yylloc.last_line,
last_line: this.yylineno+1,
first_column: this.yylloc.last_column,
last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length}
last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length};
this.yytext += match[0];
this.match += match[0];
this.matches = match;
Expand Down Expand Up @@ -343,35 +343,35 @@ _currentRules:function _currentRules() {
}});
lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {

var YYSTATE=YY_START
var YYSTATE=YY_START;
switch($avoiding_name_collisions) {
case 0:/* skip whitespace */
break;
case 1:return 6;
break;
case 2:yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2); return 4;
break;
case 3: return 17
case 3: return 17;
break;
case 4: return 18
case 4: return 18;
break;
case 5:return 23
case 5:return 23;
break;
case 6:return 24
case 6:return 24;
break;
case 7:return 22
case 7:return 22;
break;
case 8:return 21
case 8:return 21;
break;
case 9:return 10
case 9:return 10;
break;
case 10:return 11
case 10:return 11;
break;
case 11:return 8
case 11:return 8;
break;
case 12:return 14
case 12:return 14;
break;
case 13:return 'INVALID'
case 13:return 'INVALID';
break;
}
};
Expand Down Expand Up @@ -404,6 +404,6 @@ addEventListener("message", function(event) {
try {
jsonlint.parse(event.data);
} catch (errorMessage) {
postMessage(errorMessage.toString());
postMessage(JSON.stringify({error: errorMessage.toString(), loc: jsonlint.lexer.yylloc}));
}
}, false);

0 comments on commit ef88570

Please sign in to comment.