log: fix potential log decode error

This commit is contained in:
haishanh 2018-10-23 13:26:37 +08:00
parent 5aa51ae925
commit 61cde9957b
2 changed files with 10 additions and 9 deletions

View file

@ -1,10 +1,10 @@
const textDecoder = new TextDecoder('utf-8');
import { import {
getAPIConfig, getAPIConfig,
genCommonHeaders, genCommonHeaders,
getAPIBaseURL getAPIBaseURL
} from 'm/request-helper'; } from 'm/request-helper';
const endpoint = '/logs'; const endpoint = '/logs';
const textDecoder = new TextDecoder('utf-8', { stream: true });
function getURLAndInit() { function getURLAndInit() {
const c = getAPIConfig(); const c = getAPIConfig();
@ -44,13 +44,14 @@ function pump(reader) {
return; return;
} }
const t = textDecoder.decode(value); const t = textDecoder.decode(value);
let o; const arrRawJSON = t.trim().split('\n');
arrRawJSON.forEach(s => {
try { try {
o = JSON.parse(t); store.appendData(JSON.parse(s));
} catch (err) { } catch (err) {
console.log(err); console.log('JSON.parse error', JSON.parse(s));
} }
store.appendData(o); });
return pump(reader); return pump(reader);
}); });
} }

View file

@ -1,10 +1,10 @@
const textDecoder = new TextDecoder('utf-8');
import { import {
getAPIConfig, getAPIConfig,
genCommonHeaders, genCommonHeaders,
getAPIBaseURL getAPIBaseURL
} from 'm/request-helper'; } from 'm/request-helper';
const endpoint = '/traffic'; const endpoint = '/traffic';
const textDecoder = new TextDecoder('utf-8', { stream: true });
function getURLAndInit() { function getURLAndInit() {
const c = getAPIConfig(); const c = getAPIConfig();