Pre-empt EADDRINUSE and show useful error. (#407)

truffle-plugin
cgewecke 5 years ago
parent efc321e388
commit 79b6443d8e
  1. 6
      lib/api.js
  2. 4
      lib/ui.js
  3. 1
      package.json
  4. 8
      test/units/truffle/errors.js
  5. 15
      yarn.lock

@ -5,6 +5,7 @@ const path = require('path');
const istanbul = require('istanbul'); const istanbul = require('istanbul');
const util = require('util'); const util = require('util');
const assert = require('assert'); const assert = require('assert');
const detect = require('detect-port');
const ConfigValidator = require('./validator'); const ConfigValidator = require('./validator');
const Instrumenter = require('./instrumenter'); const Instrumenter = require('./instrumenter');
@ -121,6 +122,11 @@ class API {
let retry = false; let retry = false;
let address = `http://${this.host}:${this.port}`; let address = `http://${this.host}:${this.port}`;
// Check for port-in-use
if (await detect(this.port) !== this.port){
throw new Error(this.ui.generate('server-fail', [this.port]))
}
if(!this.client) this.client = client; // Prefer client from options if(!this.client) this.client = client; // Prefer client from options
this.collector = new DataCollector(this.instrumenter.instrumentationData); this.collector = new DataCollector(this.instrumenter.instrumentationData);

@ -103,6 +103,10 @@ class AppUI extends UI {
'istanbul-fail': `${c.red('Istanbul coverage reports could not be generated. ')}`, 'istanbul-fail': `${c.red('Istanbul coverage reports could not be generated. ')}`,
'sources-fail': `${c.red('Cannot locate expected contract sources folder: ')} ${args[0]}`, 'sources-fail': `${c.red('Cannot locate expected contract sources folder: ')} ${args[0]}`,
'server-fail': `${c.red('Port')} ${args[0]} ${c.red('is already in use.\n')}` +
`${c.red('\tRun: "lsof -i" to find the pid of the process using it.\n')}` +
`${c.red('\tRun: "kill -9 <pid>" to kill it.\n')}`
} }
return this._format(kinds[kind]) return this._format(kinds[kind])

@ -28,6 +28,7 @@
"chalk": "^2.4.2", "chalk": "^2.4.2",
"death": "^1.1.0", "death": "^1.1.0",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"detect-port": "^1.3.0",
"ganache-core-sc": "2.7.0-sc.0", "ganache-core-sc": "2.7.0-sc.0",
"ghost-testrpc": "^0.0.2", "ghost-testrpc": "^0.0.2",
"global-modules": "^2.0.0", "global-modules": "^2.0.0",

@ -126,7 +126,7 @@ describe('Truffle Plugin: error cases', function() {
}); });
// This case *does* throw an error, but it's uncatch-able; // This case *does* throw an error, but it's uncatch-able;
it.skip('tries to launch with a port already in use', async function(){ it('tries to launch with a port already in use', async function(){
verify.cleanInitialState(); verify.cleanInitialState();
const server = ganache.server(); const server = ganache.server();
@ -139,7 +139,11 @@ describe('Truffle Plugin: error cases', function() {
await plugin(truffleConfig); await plugin(truffleConfig);
assert.fail(); assert.fail();
} catch(err){ } catch(err){
assert(err.message.includes('EADDRINUSE: address already in use :::8545')) assert(
err.message.includes('is already in use') &&
err.message.includes('lsof'),
`Should error on port-in-use with advice: ${err.message}`
)
} }
await pify(server.close)(); await pify(server.close)();

@ -288,6 +288,11 @@ accepts@~1.3.7:
mime-types "~2.1.24" mime-types "~2.1.24"
negotiator "0.6.2" negotiator "0.6.2"
address@^1.0.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/address/-/address-1.1.2.tgz#bf1116c9c758c51b7a933d296b72c221ed9428b6"
integrity sha512-aT6camzM4xEA54YVJYSqxz1kv4IHnQZRtThJJHhUMRExaU5spC7jX5ugSwTaTgJliIgs4VhZOk7htClvQ/LmRA==
aes-js@3.0.0: aes-js@3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-3.0.0.tgz#e21df10ad6c2053295bcbb8dab40b09dbea87e4d" resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-3.0.0.tgz#e21df10ad6c2053295bcbb8dab40b09dbea87e4d"
@ -1828,7 +1833,7 @@ death@^1.1.0:
resolved "https://registry.yarnpkg.com/death/-/death-1.1.0.tgz#01aa9c401edd92750514470b8266390c66c67318" resolved "https://registry.yarnpkg.com/death/-/death-1.1.0.tgz#01aa9c401edd92750514470b8266390c66c67318"
integrity sha1-AaqcQB7dknUFFEcLgmY5DGbGcxg= integrity sha1-AaqcQB7dknUFFEcLgmY5DGbGcxg=
debug@2.6.9, debug@^2.2.0, debug@^2.6.8, debug@^2.6.9: debug@2.6.9, debug@^2.2.0, debug@^2.6.0, debug@^2.6.8, debug@^2.6.9:
version "2.6.9" version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA== integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==
@ -2029,6 +2034,14 @@ detect-indent@^4.0.0:
dependencies: dependencies:
repeating "^2.0.0" repeating "^2.0.0"
detect-port@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/detect-port/-/detect-port-1.3.0.tgz#d9c40e9accadd4df5cac6a782aefd014d573d1f1"
integrity sha512-E+B1gzkl2gqxt1IhUzwjrxBKRqx1UzC3WLONHinn8S3T6lwV/agVCyitiFOsGJ/eYuEUBvD71MZHy3Pv1G9doQ==
dependencies:
address "^1.0.1"
debug "^2.6.0"
diff@3.5.0: diff@3.5.0:
version "3.5.0" version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"

Loading…
Cancel
Save