'use strict'

let exec = require('child_process').exec
let fs = require('fs')
let stream = require('stream')
let dataStream = new stream.Readable()
dataStream._read = function () {}
dataStream.on('error', function (err) {
  console.log('Read Error: ', err)
})

let fileWriter = fs.createWriteStream('ips.txt')
fileWriter.on('error', function (err) {
  console.log('Write Error: ', err)
})

dataStream.pipe(fileWriter)

let ipfreely = function (sites) {
  sites.forEach((site) => {
    exec(`dig +short ${site}`, (err, output) => {
      if (err) {
        console.log(`Error getting site ${site}`)
        return false
      }

      dataStream.push(`${site}: ${output.match(/^[0-9\.]+/)[0]}\n`)
    })
  })
}

module.exports = ipfreely

@@ -102,16 +102,66 @@
 tream')%0A
+let errorHandler = require('./lib/error-handler')%0A
 let data
@@ -252,61 +252,20 @@
 r', 
-function (err) %7B%0A  console.log('Read Error: ', err)%0A%7D
+errorHandler
 )%0A%0Al
@@ -339,62 +339,20 @@
 r', 
-function (err) %7B%0A  console.log('Write Error: ', err)%0A%7D
+errorHandler
 )%0A%0Ad
@@ -520,48 +520,24 @@
     
-console.log(%60Error getting site $%7Bsite%7D%60
+errorHandler(err
 )%0A  

Implement basic error handler