Skip to content

Commit

Permalink
Generate user friendly message for the case when port is occupied. Do…
Browse files Browse the repository at this point in the history
… not generate unhandled exception if noone has attached `error` handler to the `LegacyPlatformProxifier`.
  • Loading branch information
azasypkin committed Aug 1, 2018
1 parent 544010c commit 885547d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/core/cli/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import chalk from 'chalk';
import { isMaster } from 'cluster';
import { accessSync, constants as fsConstants } from 'fs';
import { resolve } from 'path';
import typeDetect from 'type-detect';
import { Arguments, CommandModule, Options } from 'yargs';
import { getConfig } from '../../../server/path';
import { fromRoot } from '../../../utils/from_root';
Expand Down Expand Up @@ -244,7 +245,7 @@ export function createServeCommand(): CommandModule {
}

if (argv.port !== undefined && isNaN(argv.port)) {
throw new Error(`[port] must be a number, but was a string`);
throw new Error(`[port] must be a number, but got ${typeDetect(argv.port)}`);
}

return true;
Expand Down Expand Up @@ -276,7 +277,7 @@ function handleServeCommand(cliArgs: Arguments, installationFeatures: Installati
onShutdown: (reason?: Error | string) => {
if (reason !== undefined) {
// tslint:disable no-console
console.error(`\n${chalk.white.bgRed(' ERROR ')} ${reason}\n`);
console.error(`\n${chalk.white.bgRed(' ERROR ')} ${formatErrorMessage(reason)}\n`);
}

process.exit(reason === undefined ? 0 : (reason as any).processExitCode || 1);
Expand All @@ -302,3 +303,11 @@ function handleServeCommand(cliArgs: Arguments, installationFeatures: Installati
process.on('SIGINT', () => shutdown());
process.on('SIGTERM', () => shutdown());
}

function formatErrorMessage(reason: any) {
if (reason.code === 'EADDRINUSE' && Number.isInteger(reason.port)) {
return `Port ${reason.port} is already in use. Another instance of Kibana may be running!`;
}

return reason;
}
5 changes: 5 additions & 0 deletions src/core/server/legacy_compat/legacy_platform_proxifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export class LegacyPlatformProxifier extends EventEmitter {
return [
eventName,
(...args: any[]) => {
// We don't want to generate unhandled exception if there are no `error` listeners.
if (eventName === 'error' && this.listeners(eventName).length === 0) {
return;
}

this.log.debug(`Event is being forwarded: ${eventName}`);
this.emit(eventName, ...args);
},
Expand Down

0 comments on commit 885547d

Please sign in to comment.