EPIPE error in Node.js
Asked Answered
L

1

11

The following code run fine on JPEG, Docx, zip and several other file formats. Then I try is on mpg-filer however, I am hit by a "Error: write EPIPE" that I am unable to debug. Using a try/catch construction also result in an uncaught exception.

The code:

var fs = require('fs')
const { spawn } = require('child_process')
var file = '/path/to/some/file.jpg'

var rs = fs.createReadStream(file)
const exiftool = spawn('exiftool', ['-json', '-']);
var exif = ''

exiftool.stdout.on('data', function(chunk) {
    exif += chunk
})

exiftool.on('close', function(code) {
    console.log('Sourcefile: %s', JSON.parse(exif)[0].SourceFile)
})

exiftool.on('error', function(error) {
    console.log('exiftool has error: %s', error)
})

rs.pipe(exiftool.stdin)

The error when using mpg-files:

events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at WriteWrap.afterWrite [as oncomplete] (net.js:835:14)
Emitted 'error' event at:
    at Socket.onerror (_stream_readable.js:687:12)
    at Socket.emit (events.js:182:13)
    at onwriteError (_stream_writable.js:431:12)
    at onwrite (_stream_writable.js:456:5)
    at _destroy (internal/streams/destroy.js:40:7)
    at Socket._destroy (net.js:605:3)
    at Socket.destroy (internal/streams/destroy.js:32:8)
    at WriteWrap.afterWrite [as oncomplete] (net.js:837:10)
Lyndseylyndsie answered 14/8, 2018 at 10:28 Comment(0)
W
11

edit: corrected the code due to issues found in the comments

This kind of error can happen when you try to write to a closed stream

Try/Catch isn't the way to handle stream errors, but you can do it this way:

rs.pipe(exiftool.stdin).on('error', function(e) {
  console.log('rs.pipe has error: ' + e.message)
});
Whoosh answered 14/8, 2018 at 10:58 Comment(8)
But why does the stream close mpg's but not the other files, and how do I fix it?Lyndseylyndsie
I tried your code, but I do see "rs.pipe has error:" in the output. I get the exact same error message.Lyndseylyndsie
I get that. But your exemple would begin the output with "rs.pipe has error:" and that does not happen. I keep getting the exact same error as in the original post.Lyndseylyndsie
ok, sorry, that wasn't the right way to do it. The next should help, just try changing your last row to the next: rs.pipe(exiftool.stdin).on('error', function(e){ console.log("rs.pipe has error:" + e.message) }); The error message still doesn't say much, but with this you at least get the result and your code doesn't failWhoosh
Thanks a lot, that works. Any input as to how I can make the error not occurring? Is seems like bad practice just to ignore it.Lyndseylyndsie
It's hard to tell since there is no meaningful error output. Seems like some problem with exiftool reading .mp4 files, because it works with large .mpg files perfectly. You may try to pass your file to the exiftool in the command line directly to see if there is more meaningful error message thereWhoosh
Exiftool handles the files just fine.Lyndseylyndsie
Unfortunately, I don't know how to fix it than. EPIPE in this case means that exiftool has closed connection while you still writing to the pipe. Maybe there is something in .mp4 file structure that causing that. For example, exiftool may close the connection ones it already read the metadata it needs to output result, while nodejs still has some chunks to sendWhoosh

© 2022 - 2024 — McMap. All rights reserved.