Skip to content

Commit b05648c

Browse files
committed
refactor: start() should throw if filename doesn't exist
BREAKING CHANGE: Error handling for `start` is changing. Since `start` is an `async` function, one would expect to be able to try/catch it. The original version ran any failures through the `quit` method, but it's designed to fail gracefully and work asynchronously by using `'error'` events. This causes weird flow issues if `start` is throwing· because the filename does not exist. This change allows `start` to simply throw. Semver: major Ref: LOG-8030
1 parent 279b15e commit b05648c

File tree

3 files changed

+42
-41
lines changed

3 files changed

+42
-41
lines changed

README.md

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ const tail = new TailFile('/path/to/your/logfile.txt', {encoding: 'utf8'})
6666
.on('data', (chunk) => {
6767
console.log(`Recieved a utf8 character chunk: ${chunk}`)
6868
})
69-
.on('error', (err) => {
69+
.on('tail_error', (err) => {
7070
console.error('TailFile had an error!', err)
7171
})
72+
.on('error', (err) => {
73+
console.error('A TailFile stream error was likely encountered', err)
74+
})
7275
.start()
7376
.catch((err) => {
7477
console.error('Cannot start. Does the file exist?', err)
@@ -86,12 +89,14 @@ const TailFile = require('@logdna/tail-file')
8689
const LineSplitter = require('your-line-splitter') // There are many of these in the wild
8790

8891
const tail = new TailFile('/path/to/your/logfile.txt')
89-
.on('error', (err) => {
92+
.on('tail_error', (err) => {
9093
console.error('TailFile had an error!', err)
94+
throw err
9195
})
9296
.start()
9397
.catch((err) => {
9498
console.error('Cannot start. Does the file exist?', err)
99+
throw err
95100
})
96101

97102
// Data won't start flowing until piping
@@ -111,23 +116,33 @@ recommended since it will edge out `readline` slightly in performance.
111116

112117
```js
113118
const readline = require('readline')
114-
const tail = new TailFile('./somelog.txt')
115-
.on('error', (err) => {
116-
console.error('TailFile had an error!', err)
117-
})
118-
.start()
119-
.catch((err) => {
120-
console.error('Cannot start. Does the file exist?', err)
121-
})
119+
const TailFile = require('@logdna/tail-file')
122120

123-
const linesplitter = readline.createInterface({
124-
input: tail
125-
})
121+
async function startTail() {
122+
const tail = new TailFile('./somelog.txt')
123+
.on('tail_error', (err) => {
124+
console.error('TailFile had an error!', err)
125+
})
126+
127+
try {
128+
await tail.start()
129+
const linesplitter = readline.createInterface({
130+
input: tail
131+
})
132+
133+
linesplitter.on('line', (line) => {
134+
console.log(line)
135+
})
136+
} catch (err) {
137+
console.error('Cannot start. Does the file exist?', err)
138+
}
139+
}
126140

127-
linesplitter.on('line', (line) => {
128-
console.log(line)
141+
startTail().catch((err) => {
142+
process.nextTick(() => {
143+
throw err
144+
})
129145
})
130-
131146
```
132147

133148
## Events
@@ -242,8 +257,7 @@ Useful settings such as `encoding: 'utf8'` can be used this way.
242257
### `tail.start()`
243258

244259
* Returns: [`<Promise>`][] - Resolves after the file is polled successfully
245-
* Emits: [`<Error>`][] and [other `Readable` events](#event-any-readable-event)
246-
* Rejects: If `filename` is not found, the promise is rejected
260+
* Rejects: If `filename` is not found
247261

248262
Calling `start()` begins the polling of `filename` to watch for added/changed bytes.
249263
`start()` may be called before or after data is set up to be consumed with a

lib/tail-file.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,8 @@ class TailFile extends Readable {
107107
}
108108

109109
async start() {
110-
try {
111-
await this._openFile()
112-
await this._pollFileForChanges()
113-
} catch (err) {
114-
return this.quit(err)
115-
}
110+
await this._openFile()
111+
await this._pollFileForChanges()
116112
return
117113
}
118114

test/tail-file.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -324,23 +324,14 @@ test('Success: startPos can be provided to start tailing at a given place', (t)
324324
})
325325
})
326326

327-
test('Error: filename provided does not exist (poll error)', (t) => {
328-
t.plan(3)
329-
330-
const tail = new TailFile('THISFILEDOSNOTEXIST', {maxPollFailures: 1})
331-
.on('error', (err) => {
332-
t.type(err, Error, 'error was emitted')
333-
t.match(err, {
334-
name: 'Error'
335-
, code: 'ENOENT'
336-
, path: 'THISFILEDOSNOTEXIST'
337-
, message: /no such file or directory/
338-
})
339-
})
340-
341-
t.test('Start', async (tt) => {
342-
await tail.start()
343-
})
327+
test('Error: filename provided does not exist (throws on start)', async (t) => {
328+
const tail = new TailFile('THISFILEDOSNOTEXIST')
329+
await t.rejects(tail.start(), {
330+
name: 'Error'
331+
, code: 'ENOENT'
332+
, path: 'THISFILEDOSNOTEXIST'
333+
, message: /no such file or directory/
334+
}, 'Expected error is thrown')
344335
})
345336

346337
test('Error: poll error will retry a certain number of times', (t) => {

0 commit comments

Comments
 (0)