-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
227 additions
and
176 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {ContextWithLogger} from '../../context-with-logger'; | ||
import Driver from "./../../driver"; | ||
import {buildTestLogger} from "../../utils/tests/test-logger"; | ||
|
||
describe('driverConext', () => { | ||
|
||
it('getSafe', async () => { | ||
const {testLogger, testLoggerFn} = buildTestLogger() | ||
const ctx1 = ContextWithLogger.getSafe(testLogger, 'method1'); | ||
await ctx1.do(() => { | ||
const ctx2 = ContextWithLogger.getSafe(testLogger, 'method2'); | ||
expect(ctx2).toBe(ctx1); | ||
}); | ||
expect(testLoggerFn.mock.calls).toEqual([['trace', 'method1'], ['trace', 'method2']]); | ||
}); | ||
|
||
it('get - ok', async () => { | ||
const {testLogger, testLoggerFn} = buildTestLogger() | ||
const driver = Object.create(Driver.prototype) as Driver; | ||
(driver as any).logger = testLogger; | ||
|
||
const ctx1 = ContextWithLogger.getSafe(testLogger,'method1'); | ||
await ctx1.do(() => { | ||
const ctx2 = ContextWithLogger.get('method2'); | ||
expect(ctx2).toBe(ctx1); | ||
}); | ||
expect(testLoggerFn.mock.calls).toEqual([['trace', 'method1'], ['trace', 'method2']]); | ||
}); | ||
|
||
it('get - error', async () => { | ||
expect(() => ContextWithLogger.get('method')).toThrow(); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import {Context, getContext, NOT_A_CONTEXT} from "./utils/context"; | ||
import {Logger} from "./utils/simple-logger"; | ||
|
||
/** | ||
* Context with reference to the head object - driver. | ||
*/ | ||
export class ContextWithLogger extends Context { | ||
private constructor(context: Context, readonly logger: Logger) { | ||
super(context); | ||
} | ||
|
||
/** | ||
* This method should be called in methods that can be called by a client code - if this type of context | ||
* does not already exist, it will be created. It is important to have access to Logger object to build new context. | ||
*/ | ||
static getSafe(logger: Logger, methodName: string) { | ||
const ctx = getContext(); | ||
|
||
let context = ctx.findContextByClass<ContextWithLogger>(ContextWithLogger); | ||
|
||
if (context === NOT_A_CONTEXT) { | ||
context = new ContextWithLogger(ctx, logger); | ||
} | ||
|
||
context.trace(methodName, ctx); | ||
|
||
return context; | ||
} | ||
|
||
/** | ||
* Returns the context of this type. If there is no such context - throws an error. | ||
*/ | ||
static get(methodName: string) { | ||
const ctx = getContext(); | ||
|
||
let context = ctx.findContextByClass<ContextWithLogger>(ContextWithLogger); | ||
|
||
if (context === NOT_A_CONTEXT) { | ||
throw new Error('ContextWithLogger is not in the context chain. Consider using RiverContext.getSafe()') | ||
} | ||
|
||
context.trace(methodName, ctx); | ||
|
||
return context; | ||
} | ||
|
||
/** | ||
* Guarantees error logging if the code is called from a thread other than | ||
* the main thread, such as setTimeout or setInterval. | ||
* | ||
* An error is NOT thrown after logging. And NO result. | ||
*/ | ||
async doHandleError<T>(callback: () => T): Promise<void> { | ||
try { | ||
await super.do(callback); | ||
} catch(error) { | ||
this.logger.error(error); | ||
} | ||
} | ||
|
||
/** | ||
* Guarantees error logging if the code is called from a thread other than | ||
* the main thread, such as setTimeout or setInterval. | ||
* | ||
* An error is NOT thrown after logging. And NO result. | ||
*/ | ||
doHandleErrorSync<T>(callback: () => T): void { | ||
try { | ||
super.doSync(callback); | ||
} catch(error) { | ||
this.logger.error(error); | ||
} | ||
} | ||
|
||
/** | ||
* Writes trace to logger and creates span if tracing is enabled. | ||
*/ | ||
private trace(methodName: string, ctx: Context) { | ||
this.logger.trace(methodName, ctx); // as parameter goes las ontext in the chain | ||
// TODO: name span | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.