Skip to content

Commit

Permalink
udapted for hapi 17
Browse files Browse the repository at this point in the history
  • Loading branch information
mickelindahl committed Jan 4, 2018
1 parent d9847f1 commit 2ebb839
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ node_modules
# Ignore Idea stuff
.idea

# No npm
package-lock.json

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "6"
- "8"
- "node"


40 changes: 8 additions & 32 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'use strict';

let _server;
let debug = require('debug')('hapi-log:lib:index')

/**
* - `server` Hapi server object
Expand All @@ -17,38 +18,13 @@ let _server;
* @param {object} options
* @api public
*/
exports.register = function (server, options, next) {
exports.plugin={
register : async function (server, options) {

_server = server; // Keep a handle to server object
let _fatal=false;
_server.app.log=options.logger; //make available safely
_server = server; // Keep a handle to server object
let _fatal=false;
_server.app.log=options.logger; //make available safely

// log error as fatal at creash
process.on('uncaughtException', function (err) {

console.log('ooops');

if(_fatal){
return process.exit(1);
}

_fatal=true; //tricggers process.exit(1); when email have been sent
_server.app.log.fatal(err)

});

options.email.stream.on('sent', (msg)=>{

if(_fatal){
process.exit(1);
}

});

next();
},
pkg:require('../package.json')
};

exports.register.attributes = {
name: 'hapi-log',
version: '1.0.0'
};
53 changes: 23 additions & 30 deletions lib/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


const code = require( "code" );
const debug = require( 'debug' )( 'hapi_handlers:lib/test/index' );
const debug = require( 'debug' )( 'hapi-log:lib:test:index' );
const Lab = require( "lab" );
const serverPromise = require( './test_server.js' );
let lab = exports.lab = Lab.script();
Expand Down Expand Up @@ -60,50 +60,43 @@ function mockMailgun( server ) {
};
}

let exit = process.exit

// The order of tests matters!!!
lab.experiment( "hapi handlers", ()=> {

lab.afterEach(async ()=>{

process.exit = exit;

})

lab.test( 'Test log',
done=> {
serverPromise( options ).then( server=> {
async ()=> {
await serverPromise( options ).then( server=> {

mockMailgun( server );

server.app.log.error( 'test' )
setTimeout(
()=> {
done()
}
, 1000
);


} )
return new Promise(resolve=>{

} )
setTimeout(
()=> {
resolve()
}
, 1000
);
})

lab.test( 'Test log throw uncaughtException',
done=> {
serverPromise( options ).then( server=> {

mockMailgun( server );

let exit=process.exit;
process.exit=()=>{
server.app.log.info('exit called')
};
} ).catch(err=>{

process.emit( 'uncaughtException', { error: 'the error' } );
console.error(err)

setTimeout(
()=> {
process.exit = exit;
done()
}
, 1000
);
})

} )
} );

} )
} );
10 changes: 3 additions & 7 deletions lib/test/test_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
'use strict';

const hapiLog = require( "../index.js" );
const Hapi = require( 'hapi' );
const Promise = require( 'bluebird' );
const debug = require( 'debug' )( 'hapi_account:test_server' );
const Hapi = require( 'hapi' );;

function getServerPromise( options ) {
let server = new Hapi.Server();

server.connection( { host: '0.0.0.0', port: 3000 } );
let server = new Hapi.Server({ host: '0.0.0.0', port: 3000 });

return new Promise( resolve=> {

server.register( [
{
register: hapiLog,
plugin: hapiLog,
options: options
}

Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
},
"homepage": "https://github.com/mickelindahl/hapi_log#readme",
"devDependencies": {
"bluebird": "^3.4.7",
"bunyan": "^1.8.5",
"bunyan-mailgun": "0.0.1",
"bunyan-prettystream": "^0.1.3",
"code": "^4.0.0",
"coveralls": "^2.11.15",
"debug": "^2.5.2",
"hapi": "^16.0.2",
"lab": "^11.2.1"
"code": "^5.1.2",
"coveralls": "^3.0.0",
"debug": "^3.1.0",
"hapi": "^17.2.0",
"lab": "^15.1.2"
}
}

0 comments on commit 2ebb839

Please sign in to comment.