EventMediator: Log error stack under context
parent
e885026e0a
commit
84c2601f1c
|
@ -70,8 +70,8 @@ export class EventMediator
|
||||||
|
|
||||||
private _handleError( e: any ): void
|
private _handleError( e: any ): void
|
||||||
{
|
{
|
||||||
let msg: string = '';
|
let msg: string = '';
|
||||||
let context = {};
|
let context: Record<string, any> = {};
|
||||||
|
|
||||||
if ( e instanceof( Error ) )
|
if ( e instanceof( Error ) )
|
||||||
{
|
{
|
||||||
|
@ -81,6 +81,8 @@ export class EventMediator
|
||||||
{
|
{
|
||||||
context = e.context;
|
context = e.context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context.stack = e.stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._log.error( msg, context );
|
this._log.error( msg, context );
|
||||||
|
|
|
@ -96,27 +96,33 @@ describe( 'system.EventLogger captures and logs events', () =>
|
||||||
expect( method_called ).to.be.true;
|
expect( method_called ).to.be.true;
|
||||||
} );
|
} );
|
||||||
|
|
||||||
it( 'context is retrieved from error', () =>
|
it( 'context and stack are retrieved from error', () =>
|
||||||
{
|
{
|
||||||
let method_called = false;
|
let method_called = false;
|
||||||
|
|
||||||
const event_id = 'error';
|
const event_id = 'error';
|
||||||
const err_msg = 'Foo';
|
const err_msg = 'Foo';
|
||||||
|
const stub_err = new Error( err_msg );
|
||||||
const emitter = new EventEmitter();
|
const emitter = new EventEmitter();
|
||||||
const log = createMockLogger();
|
const log = createMockLogger();
|
||||||
const err_context = { bar: 'baz' };
|
const err_context = { bar: 'baz' };
|
||||||
|
|
||||||
|
const expected_context = {
|
||||||
|
bar: err_context.bar,
|
||||||
|
stack: stub_err.stack,
|
||||||
|
};
|
||||||
|
|
||||||
log.error = ( str: string, context: any ) =>
|
log.error = ( str: string, context: any ) =>
|
||||||
{
|
{
|
||||||
method_called = true;
|
|
||||||
|
|
||||||
expect( str ).to.equal( err_msg );
|
expect( str ).to.equal( err_msg );
|
||||||
expect( context ).to.equal( err_context );
|
expect( context ).to.deep.equal( expected_context );
|
||||||
|
|
||||||
|
method_called = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
new Sut( log, emitter );
|
new Sut( log, emitter );
|
||||||
|
|
||||||
emitter.emit( event_id, context( new Error( err_msg ), err_context ) );
|
emitter.emit( event_id, context( stub_err, err_context ) );
|
||||||
|
|
||||||
expect( method_called ).to.be.true;
|
expect( method_called ).to.be.true;
|
||||||
} );
|
} );
|
||||||
|
@ -136,4 +142,4 @@ function createMockLogger(): PsrLogger
|
||||||
emergency( _msg: string | object, _context: object ){},
|
emergency( _msg: string | object, _context: object ){},
|
||||||
log( _level: any, _msg: string | object, _context: object ){},
|
log( _level: any, _msg: string | object, _context: object ){},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue