         output.append(data.toString())
         finishCommand()
 
+      serialPort.on 'error', (error) -> commandExecuted.reject(error)
+
       serialPort.open -> connected.resolve(serialPort)
 
     if !config.port && !config.serialNumber


    through.obj (chunk, encoding, callback) ->
      publish = createPublisher(@, callback)
      onError = (error) -> publish.error(error)

      espruino.connect()
         .then(-> espruino.send("reset();\n", onError) if config.reset)
         .then(-> espruino.send("echo(0);\n", onError) if config.echoOff)
         .then(-> espruino.send("{ #{chunk.contents.toString()} }\n", onError))
         .then(-> espruino.send("echo(1);\n", onError) if config.echoOff)
         .then(-> espruino.send("save();\n", onError) if config.save)
         .then(-> publish.content(espruino.log()))
         .fail((error) -> publish.error(error))
         .finally(-> espruino.close())

# Todo.
# blow up if not stream
# add in hook to finish stream slurp whenever you want
# add timeout to promise chain
