Permalink
Browse files

Automatically try to fix some common escape errors from copy-pasted l…

…og entries
  • Loading branch information...
icewind1991 committed Feb 9, 2017
1 parent 5a62e7b commit 031db2a7f9b9f8cee42acf6eae7d993d31d4660a
Showing with 74 additions and 24 deletions.
  1. +22 −20 build/main.js
  2. +1 −1 build/main.js.map
  3. +26 −2 js/ExceptionParser.js
  4. +25 −1 js/Providers/LogFile.js
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -18,9 +18,15 @@ export class ExceptionParser {
parse (logMessage) {
let data;
if (this.isRegularException(logMessage)) {
data = JSON.parse(logMessage.substr(10));
try {
data = this.tryParseJSON(logMessage.substr(10));
} catch (e) {
console.log('Error while parsing exception:');
console.log(logMessage.substr(10));
console.error(e);
}
} else {
data = JSON.parse(logMessage.substr(logMessage.indexOf('{"Exception":')));
data = this.tryParseJSON(logMessage.substr(logMessage.indexOf('{"Exception":')));
const messageHead = logMessage.substr(0, logMessage.indexOf('{"Exception":'));
const jobDataString = messageHead.split('(', 2)[1];
const jobDataParts = jobDataString.split(',', 2).map(part=>part.trim());
@@ -40,6 +46,24 @@ export class ExceptionParser {
return data;
}
tryParseJSON(json) {
try {
return JSON.parse(json);
} catch (e) {
// fix unescaped newlines
json = json.replace(/\n/g, '');
// fix unescaped namespace delimiters
json = json.replace(/([^\\])\\([A-Z{])/g, '$1\\\\$2');
try {
return JSON.parse(json);
} catch (e) {
console.log('Error while parsing exception:');
console.log(json);
console.error(e);
}
}
}
parseCommandJob (data) {
const parsed = unserialize(data);
return [parsed['class'], parsed.properties];
View
@@ -10,7 +10,31 @@ export class LogFile extends LogProvider {
async loadEntries (offset, count = 50) {
const start = this.lines.length - offset;
const end = Math.max(start - count - 2, 0);
const entries = this.lines.slice(end, start).reverse().map(JSON.parse);
const entries = this.lines.slice(end, start).reverse().map(this.tryParseJSON);
return {data: entries};
}
tryParseJSON (json) {
try {
return JSON.parse(json);
} catch (e) {
// fix unescaped message json
const startPos = json.indexOf('"message":"') + ('"message":"').length;
const endPos = json.lastIndexOf('","level":');
const start = json.substr(0, startPos);
const end = json.substr(endPos);
const message = json.substr(startPos, endPos - startPos);
const escapedMessage = message.replace(/([^\\]|^)["]/g, '$1\\"');
json = start + escapedMessage + end;
try {
return JSON.parse(json);
} catch (e) {
console.log('Error while parsing log message:');
console.log(json);
console.error(e);
}
}
}
}

0 comments on commit 031db2a

Please sign in to comment.