From 36d1cf629fc4cee2a0c508c8da21734b0e1b464d Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sun, 27 May 2018 13:05:38 -0700 Subject: [PATCH] fix file delete (unlink) so catches error uses promise --- package.json | 14 +++++++------- src/consumer.js | 4 ++-- src/socket.js | 11 ++++++++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 1ade8cd..d8db821 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uci/socket", - "version": "0.1.3", + "version": "0.1.4", "description": "JSON packet intra(named)/inter(TCP) host communication over socket", "main": "src", "scripts": { @@ -38,17 +38,17 @@ "devDependencies": { "chai": "^4.1.2", "chai-as-promised": "^7.1.1", - "codecov": "^3.0.0", - "esm": "^3.0.21", + "codecov": "^3.0.2", + "esm": "^3.0.37", "istanbul": "^0.4.5", - "mocha": "^5.0.1", - "nodemon": "^1.15.1" + "mocha": "^5.2.0", + "nodemon": "^1.17.5" }, "dependencies": { - "@uci/logger": "^0.0.2", + "@uci/logger": "0.0.3", "better-try-catch": "^0.6.2", "clone": "^2.1.1", "death": "^1.1.0", - "make-dir": "^1.2.0" + "make-dir": "^1.3.0" } } diff --git a/src/consumer.js b/src/consumer.js index ea258e2..fc7f31a 100644 --- a/src/consumer.js +++ b/src/consumer.js @@ -105,7 +105,7 @@ export default class Consumer extends Socket { let res = await this._packetProcess(reply) if (!res) { // if process was not promise returning like just logged to console res = reply - log.warn('consumer function was not promise returning - resolving unprocessed') + log.warn('consumer function was not promise returning further processing may be out of sequence') } resolve(res) }) //end listener @@ -143,7 +143,7 @@ export default class Consumer extends Socket { // TODO do some extra security here? let res = await this._packetProcess(packet) if (!res) { // if process was not promise returning like just logged to console - log.warn('consumer function was not promise returning - resolving unprocessed') + log.warn('consumer function was not promise returning') } }) // listen on socket stream diff --git a/src/socket.js b/src/socket.js index ccf8df9..b4268fa 100644 --- a/src/socket.js +++ b/src/socket.js @@ -1,5 +1,6 @@ import { Server } from 'net' import { unlink as fileDelete } from 'fs' +import { promisify } from 'util' import path from 'path' import mkdir from 'make-dir' import btc from 'better-try-catch' @@ -52,9 +53,13 @@ export default class Socket extends Server { // recover from socket file that was not removed if (err.code === 'EADDRINUSE') { if (this.opts.path) { // if TCP socket should already be dead - log.warn({socket: this.opts.path}, 'socket already exists...deleting') - await fileDelete(this.opts.path) - return await this._listen(this.opts) + let [err, res] = log.info(await btc(promisify(fileDelete))(this.opts.path)) + if(!err) { + log.info({res:res, socket: this.opts.path}, 'socket already exists.....deleted') + return await this._listen(this.opts) + } + log.fatal({err:err},'error deleting socket. Can not establish a socket') + return err } } if (err.code ==='EACCES'){