From c28bf93d545d86668a320acaba4821fdeea97f71 Mon Sep 17 00:00:00 2001 From: Jeroen Plug Date: Wed, 15 Sep 2021 14:40:03 +0200 Subject: [PATCH] feat(Context): Add lastError field to request context --- packages/easy-express/src/types/NamespaceContext.ts | 8 ++++++++ .../easy-express/test/types/NamespaceContext.test.ts | 2 ++ packages/easy/src/types/Context.ts | 9 +++++++++ 3 files changed, 19 insertions(+) diff --git a/packages/easy-express/src/types/NamespaceContext.ts b/packages/easy-express/src/types/NamespaceContext.ts index 43dd784e9..be81d7562 100644 --- a/packages/easy-express/src/types/NamespaceContext.ts +++ b/packages/easy-express/src/types/NamespaceContext.ts @@ -32,6 +32,14 @@ export class NamespaceContext implements RequestContext { this.namespace.set('correlationId', id); } + get lastError(): string | undefined { + return this.namespace.get('lastError'); + } + + set lastError(error: string | undefined) { + this.namespace.set('lastError', error); + } + public get(key: string): any { return this.namespace.get(key); } diff --git a/packages/easy-express/test/types/NamespaceContext.test.ts b/packages/easy-express/test/types/NamespaceContext.test.ts index c543f4767..048add451 100644 --- a/packages/easy-express/test/types/NamespaceContext.test.ts +++ b/packages/easy-express/test/types/NamespaceContext.test.ts @@ -30,8 +30,10 @@ describe('NamespaceContext', () => { context.create(() => { context.token = 'token'; context.correlationId = 'correlation'; + context.lastError = 'error'; expect(context.token).toBe('token'); expect(context.correlationId).toBe('correlation'); + expect(context.lastError).toBe('error'); }); }); }); diff --git a/packages/easy/src/types/Context.ts b/packages/easy/src/types/Context.ts index 05b18722a..37af7dda1 100644 --- a/packages/easy/src/types/Context.ts +++ b/packages/easy/src/types/Context.ts @@ -30,6 +30,7 @@ export type RequestContext = { identity?: Identity; jwt: string; correlationId?: Uuid; + lastError?: string; create: (f: () => void) => void; }; @@ -64,6 +65,14 @@ export class BaseContext implements RequestContext { this.state.correlationId = id; } + get lastError(): string | undefined { + return this.state.lastError; + } + + set lastError(error: string | undefined) { + this.state.lastError = error; + } + public get(key: string): any { return this.state[key]; }