From 7d000b756b40c3763e0d5052275d4e308bd2895d Mon Sep 17 00:00:00 2001 From: Garrett Downs Date: Sat, 13 Apr 2024 10:15:22 -0400 Subject: [PATCH] refine logging --- package.json | 2 +- src/lib/connection.ts | 10 ++-------- src/lib/kaiware.ts | 29 ++++++----------------------- src/types/Log.ts | 2 +- src/types/LogMessageData.ts | 8 ++++++++ src/types/index.ts | 1 + src/utils/index.ts | 1 + src/utils/isJson.ts | 4 ++-- src/utils/parseError.ts | 13 +++++++++++++ 9 files changed, 35 insertions(+), 35 deletions(-) create mode 100644 src/types/LogMessageData.ts create mode 100644 src/utils/parseError.ts diff --git a/package.json b/package.json index d0c4c77..d00c9b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nothing-special/kaiware-lib", - "version": "0.2.0", + "version": "0.3.0", "type": "module", "author": { "name": "Garrett Downs", diff --git a/src/lib/connection.ts b/src/lib/connection.ts index c1db75e..335a01d 100644 --- a/src/lib/connection.ts +++ b/src/lib/connection.ts @@ -1,6 +1,6 @@ import { z } from 'zod'; -import { LogLevel, MessageType } from '../enums'; -import { Config, Log, Message } from '../types'; +import { MessageType } from '../enums'; +import { Config, Message } from '../types'; import { isJson } from '../utils'; export class Connection { @@ -22,12 +22,6 @@ export class Connection { this.socket.onopen = () => { console.log('WebSocket connection established'); - this.sendMessage>(MessageType.NewLog, { - source: this.config.sourceId, - level: LogLevel.Info, - data: ['Connection established'], - timestamp: new Date().toISOString() - }); resolve(); }; diff --git a/src/lib/kaiware.ts b/src/lib/kaiware.ts index 9533b88..66b3c52 100644 --- a/src/lib/kaiware.ts +++ b/src/lib/kaiware.ts @@ -1,5 +1,6 @@ import { LogLevel, MessageType } from '../enums'; -import { Config, Log } from '../types'; +import { Config, LogMessageData } from '../types'; +import { parseError } from '../utils'; import { Connection } from './connection'; export class Kaiware { @@ -44,39 +45,21 @@ export class Kaiware { const stringifiedData: string[] = data.map((a) => { if (typeof a === 'string') { return a; - } else if (a instanceof Error) { - return JSON.stringify(a, [ - 'message', - 'type', - 'name', - 'stack', - 'fileName', - 'lineNumber', - 'columnNumber' - ]); - } else if (a instanceof ErrorEvent) { - return JSON.stringify(a, [ - 'message', - 'type', - 'name', - 'stack', - 'fileName', - 'lineNumber', - 'columnNumber' - ]); + } else if (a instanceof Error || a instanceof ErrorEvent) { + return JSON.stringify(parseError(a)); } else { return JSON.stringify(a); } }); - const log: Omit = { + const log: LogMessageData = { source: this.config!.sourceId, level: level, data: stringifiedData, timestamp: new Date().toISOString() }; - this.connection.sendMessage(MessageType.NewLog, log); + this.connection.sendMessage(MessageType.NewLog, log); } static log = { diff --git a/src/types/Log.ts b/src/types/Log.ts index dd2f937..51233be 100644 --- a/src/types/Log.ts +++ b/src/types/Log.ts @@ -4,6 +4,6 @@ export type Log = { id: number; source: string; level: LogLevel; - data: string[]; + data: unknown[]; timestamp: string; }; diff --git a/src/types/LogMessageData.ts b/src/types/LogMessageData.ts new file mode 100644 index 0000000..a785ce6 --- /dev/null +++ b/src/types/LogMessageData.ts @@ -0,0 +1,8 @@ +import { LogLevel } from '../enums'; + +export type LogMessageData = { + source: string; + level: LogLevel; + data: string[]; + timestamp: string; +}; diff --git a/src/types/index.ts b/src/types/index.ts index 4dc932a..315b21d 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,5 @@ export * from './Config'; export * from './DeviceInfo'; export * from './Log'; +export * from './LogMessageData'; export * from './Message'; diff --git a/src/utils/index.ts b/src/utils/index.ts index 2200346..22012c0 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1 +1,2 @@ export * from './isJson'; +export * from './parseError'; diff --git a/src/utils/isJson.ts b/src/utils/isJson.ts index 1c354a4..1eb1861 100644 --- a/src/utils/isJson.ts +++ b/src/utils/isJson.ts @@ -1,6 +1,6 @@ -export function isJson(data: string): boolean { +export function isJson(data: unknown): boolean { try { - JSON.parse(data); + JSON.parse(data as string); } catch (error) { return false; } diff --git a/src/utils/parseError.ts b/src/utils/parseError.ts new file mode 100644 index 0000000..b365b90 --- /dev/null +++ b/src/utils/parseError.ts @@ -0,0 +1,13 @@ +export function parseError(err: Error | ErrorEvent): object { + return JSON.parse( + JSON.stringify(err, [ + 'message', + 'type', + 'name', + 'stack', + 'fileName', + 'lineNumber', + 'columnNumber' + ]) + ); +}