Skip to content

Commit

Permalink
throw error (uncaughtException) instead of exit the process
Browse files Browse the repository at this point in the history
  • Loading branch information
timaschew committed Jul 5, 2016
1 parent 98e51dc commit 743e578
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
5 changes: 3 additions & 2 deletions bin/deepstream-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function action() {
detached: true,
stdio: [ 'ignore']
} );
const WAIT_FOR_ERRORS = 2000;
const WAIT_FOR_ERRORS = 3000;
// register handler if the child process will fail within WAIT_FOR_ERRORS period
child.on( 'close', detachErrorHandler );
child.on( 'exit', detachErrorHandler );
Expand All @@ -77,6 +77,7 @@ function action() {
// non-detach casee
const Deepstream = require( '../src/deepstream.io.js' );
try {
process.on( 'uncaughtException', pidHelper.exit );
var ds = new Deepstream( null );
ds.on( 'started', function() {
pidHelper.save( process.pid );
Expand All @@ -93,7 +94,7 @@ function action() {
}

function detachErrorHandler() {
console.error( 'Error during detaching the deepstream process, run without --detach'.red );
console.error( 'Error during detaching the deepstream process, see logs or run without --detach'.red );
process.exit( 1 );
}

Expand Down
2 changes: 2 additions & 0 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ exports.EVENT.INVALID_MESSAGE = 'INVALID_MESSAGE';
exports.EVENT.VERSION_EXISTS = 'VERSION_EXISTS';
exports.EVENT.INVALID_VERSION = 'INVALID_VERSION';
exports.EVENT.PLUGIN_ERROR = 'PLUGIN_ERROR';
exports.EVENT.PLUGIN_INITIALIZATION_ERROR = 'PLUGIN_INITIALIZATION_ERROR';
exports.EVENT.PLUGIN_INITIALIZATION_TIMEOUT = 'PLUGIN_INITIALIZATION_TIMEOUT';
exports.EVENT.UNKNOWN_CALLEE = 'UNKNOWN_CALLEE';

exports.TOPIC = {};
Expand Down
11 changes: 8 additions & 3 deletions src/utils/dependency-initialiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ DependencyInitialiser.prototype._onReady = function() {
* @returns {void}
*/
DependencyInitialiser.prototype._onTimeout = function() {
this._logError( this._name + ' wasn\'t initialised in time' );
process.exit( 1 );
const message = this._name + ' wasn\'t initialised in time';
this._logError( message );
const error = new Error( message );
error.code = C.EVENT.PLUGIN_INITIALIZATION_TIMEOUT;
throw error;

};

/**
Expand All @@ -84,7 +88,8 @@ DependencyInitialiser.prototype._onTimeout = function() {
DependencyInitialiser.prototype._onError = function( error ) {
if( this.isReady !== true ) {
this._logError( 'Error while initialising ' + this._name + ': ' + error.toString() );
process.exit( 1 );
error.code = C.EVENT.PLUGIN_INITIALIZATION_ERROR;
throw error;
}
};

Expand Down
48 changes: 20 additions & 28 deletions test/utils/dependency-initialiserSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,59 +41,51 @@ describe( 'encounters timeouts and errors during dependency initialisations', fu
var onReady = jasmine.createSpy( 'onReady' );
var exit = jasmine.createSpy( 'exit');
var log = jasmine.createSpy( 'log' );
var originalProcessExit = process.exit;
var originalConsoleLog = console.log;
var options = {
plugin: new PluginMock( 'A' ),
logger: { log: jasmine.createSpy( 'log' ), isReady: true },
dependencyInitialisationTimeout: 1
};

it( 'disables process exit', function(){
Object.defineProperty( process, 'exit', {
value: exit
});
it( 'disables console.error', function(){

Object.defineProperty( console, 'error', {
value: log
});
});

it( 'creates a depdendency initialiser', function( next ){
it( 'creates a depdendency initialiser and doesnt initialise a plugin in time', function( next ){
dependencyInitialiser = new DependencyInitialiser( options, 'plugin' );
dependencyInitialiser.on( 'ready', onReady );
expect( options.plugin.isReady ).toBe( false );
setTimeout( next, 5 );
});

it( 'doesnt initialise a plugin in time', function(){
process.once( 'uncaughtException', function() {
expect( options.logger.log ).toHaveBeenCalledWith( 3, 'PLUGIN_ERROR', 'plugin wasn\'t initialised in time' );
next();
} );
expect( onReady ).not.toHaveBeenCalled();
expect( exit ).toHaveBeenCalled();
expect( options.logger.log ).toHaveBeenCalledWith( 3, 'PLUGIN_ERROR', 'plugin wasn\'t initialised in time' );

});

it( 'creates another depdendency initialiser', function( next ){
it( 'creates another depdendency initialiser with a plugin error', function( next ){
process.once( 'uncaughtException', function(err) {
expect( onReady ).not.toHaveBeenCalled();
expect( log ).toHaveBeenCalledWith( 'Error while initialising dependency' );
expect( log ).toHaveBeenCalledWith( 'Error while initialising plugin: something went wrong' );
next();
} );
dependencyInitialiser = new DependencyInitialiser( options, 'plugin' );
dependencyInitialiser.on( 'ready', onReady );
options.logger.isReady = false;
options.plugin.emit( 'error', 'something went wrong' );
setTimeout( next , 50 );
});

it( 'has logged the plugin error', function(){
expect( exit ).toHaveBeenCalled();
expect( onReady ).not.toHaveBeenCalled();
expect( log ).toHaveBeenCalledWith( 'Error while initialising dependency' );
expect( log ).toHaveBeenCalledWith( 'Error while initialising plugin: something went wrong' );
try {
options.plugin.emit( 'error', 'something went wrong' );
next.fail();
} catch (_err) {}
});

it( 'enables process exit', function(){
Object.defineProperty( process, 'exit', {
value: originalProcessExit
});

it( 'enable console.error', function(){
Object.defineProperty( console, 'error', {
value: originalConsoleLog
});
});
});
});

0 comments on commit 743e578

Please sign in to comment.