Skip to content

Commit

Permalink
ensure connection errors cause promise rejection. remove excessive logs
Browse files Browse the repository at this point in the history
  • Loading branch information
evanshortiss committed Dec 11, 2016
1 parent 5e94147 commit 5c38d92
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
40 changes: 14 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ var serialport = require('serialport')
, VError = require('verror')
, conn = null
, assert = require('assert')
, fhlog = require('fhlog')
, log = fhlog.get('[obd-serial-connection]');
, debug = require('debug')(require('./package.json').name);


// Keep track of connection requests
Expand Down Expand Up @@ -49,13 +48,13 @@ module.exports = function (opts) {
);

return new Promise(function (resolve, reject) {
log.d('creating serialport connection');
debug('creating serialport connection');

if (conn && conn.ready) {
log.d('returning existing connection instance');
debug('returning existing connection instance');
resolve(conn);
} else {
log.d('opening a serial connection');
debug('opening a serial connection');

// Keep track of the promise(s) we're returning
connQ.push({
Expand All @@ -70,33 +69,22 @@ module.exports = function (opts) {
conn.on('open', function () {
onConnectionOpened(configureFn);
});

conn.on('error', function (err) {
onConnectionOpened(configureFn, err);
});
}
});
};
};


/**
* Logger instance used by the applicaton
* @type {fhlog.Logger}
*/
module.exports.logger = log;


/**
* The fhlog module instance used by this module
* @type {Object}
*/
module.exports.fhlog = fhlog;


/**
* Parses serial data and emits and event related to the PID of the data.
* Pollers will listen for events related to their PID
* @param {String} str
*/
function onSerialData (str) {
log.d('received obd data %s', str);
debug('received obd data %s', str);
}


Expand All @@ -121,8 +109,8 @@ function respondToConnectionRequests (err) {
* @param {Erorr} err
*/
function onSerialError (err) {
log.e('serial emitted an error %s', err.toString());
log.e(err.stack);
debug('serial emitted an error %s', err.toString());
debug(err.stack);
}


Expand All @@ -138,19 +126,19 @@ function onConnectionOpened (configureFn, err) {
if (err) {
err = new VError(err, 'failed to connect to ecu');

log.e('error establishing a serial connection: %s', err);
debug('error establishing a serial connection: %s', err);

respondToConnectionRequests(err);
} else {
log.d('serial connection established, running configuration function');
debug('serial connection established, running configuration function');

// Bind listeners for data and errors
conn.on('error', onSerialError);
conn.on('data', onSerialData);

return configureFn(conn)
.then(function onConfigurationComplete () {
log.i('finished running configuration function, returning connection');
debug('finished running configuration function, returning connection');

conn.ready = true;

Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obd-parser-serial-connection",
"version": "0.1.1",
"version": "0.1.2",
"description": "A serial connection wrapper for use with odb-parser",
"main": "index.js",
"scripts": {
Expand All @@ -12,13 +12,18 @@
"connection",
"obd2",
"obdii",
"serialport"
"elm327",
"serialport",
"vehicle",
"diagnostics",
"can",
"car"
],
"author": "Evan Shortiss",
"license": "MIT",
"dependencies": {
"bluebird": "~3.3.1",
"fhlog": "~0.12.1",
"debug": "~2.3.3",
"serialport": "~2.1.0",
"verror": "~1.6.1",
"xtend": "~4.0.1"
Expand Down
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ describe('obd-serial-connection', function () {
function SerialPort () {
EventEmitter.call(this);

this.open = function (done) {
setTimeout((function () {
if (err) {
done(new Error('fake error'), null);
this.emit('error', new Error('fake error'));
} else {
done(null, {});
this.emit('open');
}
}
}).bind(this));
}
util.inherits(SerialPort, EventEmitter);

Expand Down

0 comments on commit 5c38d92

Please sign in to comment.