Skip to content
This repository was archived by the owner on May 15, 2019. It is now read-only.

Commit ca9200b

Browse files
Ace Nassrijmdobry
authored andcommitted
Handle missing logfiles correctly (#199)
* Fix missing logfile error handling not working * Fix lint
1 parent d38d0a0 commit ca9200b

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/emulator/logs.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,35 @@ const pkg = require('../../package.json');
2727
const defaultLogsDir = path.join(xdgBasedir.config || os.tmpdir(), pkg.name);
2828

2929
function readLogLines (filePath, linesToRead, output) {
30-
try {
31-
const parts = path.parse(filePath);
32-
const files = fs
33-
.readdirSync(parts.dir)
34-
.filter((file) => file && file.includes(parts.name));
35-
files.sort();
30+
const parts = path.parse(filePath);
31+
const files = fs
32+
.readdirSync(parts.dir)
33+
.filter((file) => file && file.includes(parts.name));
34+
files.sort();
3635

37-
// Here, we naively select the newest log file, even if the user wants to
38-
// display more lines than are available in the newest log file.
39-
const rl = readline.createInterface({
40-
input: fs.createReadStream(path.join(parts.dir, files[files.length - 1])),
41-
terminal: false
42-
});
43-
const lines = [];
44-
rl
45-
.on('line', (line) => {
46-
lines.push(line);
47-
})
48-
.on('close', () => {
49-
lines
50-
.slice(lines.length - linesToRead)
51-
.forEach((line) => output(`${line}\n`));
52-
});
53-
} catch (err) {
36+
// Here, we naively select the newest log file, even if the user wants to
37+
// display more lines than are available in the newest log file.
38+
const stream = fs.createReadStream(path.join(parts.dir, files[files.length - 1] || ''));
39+
stream.on('error', (err) => {
5440
if (err.code === 'ENOENT') {
5541
output('');
56-
return;
5742
}
43+
});
5844

59-
throw err;
60-
}
45+
const rl = readline.createInterface({
46+
input: stream,
47+
terminal: false
48+
});
49+
const lines = [];
50+
rl
51+
.on('line', (line) => {
52+
lines.push(line);
53+
})
54+
.on('close', () => {
55+
lines
56+
.slice(lines.length - linesToRead)
57+
.forEach((line) => output(`${line}\n`));
58+
});
6159
}
6260

6361
module.exports = {

0 commit comments

Comments
 (0)