diff --git a/deno-runtime/AppObjectRegistry.ts b/deno-runtime/AppObjectRegistry.ts index d75e78460..9069c17ea 100644 --- a/deno-runtime/AppObjectRegistry.ts +++ b/deno-runtime/AppObjectRegistry.ts @@ -1,8 +1,10 @@ +export type Maybe = T | null | undefined; + export const AppObjectRegistry = new class { registry: Record = {}; - public get(key: string): unknown { - return this.registry[key]; + public get(key: string): Maybe { + return this.registry[key] as Maybe; } public set(key: string, value: unknown): void { diff --git a/deno-runtime/lib/logger.ts b/deno-runtime/lib/logger.ts index 53bee44a5..bf84a176f 100644 --- a/deno-runtime/lib/logger.ts +++ b/deno-runtime/lib/logger.ts @@ -78,7 +78,7 @@ export class Logger { } const str = JSON.stringify(args, null, 2); return str ? JSON.parse(str) : str; // force call toJSON to prevent circular references - + }); this.entries.push({ @@ -116,6 +116,10 @@ export class Logger { return new Date().getTime() - this.start.getTime(); } + public hasEntries(): boolean { + return this.entries.length > 0; + } + public getLogs(): ILoggerStorageEntry { return { appId: this.appId, diff --git a/deno-runtime/lib/messenger.ts b/deno-runtime/lib/messenger.ts index 97dd43859..1baf34d1d 100644 --- a/deno-runtime/lib/messenger.ts +++ b/deno-runtime/lib/messenger.ts @@ -73,17 +73,26 @@ export async function sendMethodNotFound(id: jsonrpc.ID): Promise { } export async function errorResponse({ error: { message, code = -32000, data }, id }: ErrorResponseDescriptor): Promise { - const logger = AppObjectRegistry.get('logger') as Logger; - const logs = logger.getLogs(); - const rpc = jsonrpc.error(id, new jsonrpc.JsonRpcError(message, code, {logs, ...data})); + const logger = AppObjectRegistry.get('logger'); + + if (logger?.hasEntries()) { + data.logs = logger.getLogs(); + } + + const rpc = jsonrpc.error(id, new jsonrpc.JsonRpcError(message, code, data)); await send(rpc); } export async function successResponse({ id, result }: SuccessResponseDescriptor): Promise { - const logger = AppObjectRegistry.get('logger') as Logger; - const logs = logger.getLogs(); - const rpc = jsonrpc.success(id, {value: result, logs}); + const payload = { value: result } as Record; + const logger = AppObjectRegistry.get('logger'); + + if (logger?.hasEntries()) { + payload.logs = logger.getLogs(); + } + + const rpc = jsonrpc.success(id, payload); await send(rpc); }