Skip to content

Commit 7d000b7

Browse files
committed
refine logging
1 parent ed41ace commit 7d000b7

9 files changed

Lines changed: 35 additions & 35 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nothing-special/kaiware-lib",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"type": "module",
55
"author": {
66
"name": "Garrett Downs",

src/lib/connection.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { z } from 'zod';
2-
import { LogLevel, MessageType } from '../enums';
3-
import { Config, Log, Message } from '../types';
2+
import { MessageType } from '../enums';
3+
import { Config, Message } from '../types';
44
import { isJson } from '../utils';
55

66
export class Connection {
@@ -22,12 +22,6 @@ export class Connection {
2222

2323
this.socket.onopen = () => {
2424
console.log('WebSocket connection established');
25-
this.sendMessage<Omit<Log, 'id'>>(MessageType.NewLog, {
26-
source: this.config.sourceId,
27-
level: LogLevel.Info,
28-
data: ['Connection established'],
29-
timestamp: new Date().toISOString()
30-
});
3125
resolve();
3226
};
3327

src/lib/kaiware.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LogLevel, MessageType } from '../enums';
2-
import { Config, Log } from '../types';
2+
import { Config, LogMessageData } from '../types';
3+
import { parseError } from '../utils';
34
import { Connection } from './connection';
45

56
export class Kaiware {
@@ -44,39 +45,21 @@ export class Kaiware {
4445
const stringifiedData: string[] = data.map((a) => {
4546
if (typeof a === 'string') {
4647
return a;
47-
} else if (a instanceof Error) {
48-
return JSON.stringify(a, [
49-
'message',
50-
'type',
51-
'name',
52-
'stack',
53-
'fileName',
54-
'lineNumber',
55-
'columnNumber'
56-
]);
57-
} else if (a instanceof ErrorEvent) {
58-
return JSON.stringify(a, [
59-
'message',
60-
'type',
61-
'name',
62-
'stack',
63-
'fileName',
64-
'lineNumber',
65-
'columnNumber'
66-
]);
48+
} else if (a instanceof Error || a instanceof ErrorEvent) {
49+
return JSON.stringify(parseError(a));
6750
} else {
6851
return JSON.stringify(a);
6952
}
7053
});
7154

72-
const log: Omit<Log, 'id'> = {
55+
const log: LogMessageData = {
7356
source: this.config!.sourceId,
7457
level: level,
7558
data: stringifiedData,
7659
timestamp: new Date().toISOString()
7760
};
7861

79-
this.connection.sendMessage(MessageType.NewLog, log);
62+
this.connection.sendMessage<LogMessageData>(MessageType.NewLog, log);
8063
}
8164

8265
static log = {

src/types/Log.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export type Log = {
44
id: number;
55
source: string;
66
level: LogLevel;
7-
data: string[];
7+
data: unknown[];
88
timestamp: string;
99
};

src/types/LogMessageData.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { LogLevel } from '../enums';
2+
3+
export type LogMessageData = {
4+
source: string;
5+
level: LogLevel;
6+
data: string[];
7+
timestamp: string;
8+
};

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './Config';
22
export * from './DeviceInfo';
33
export * from './Log';
4+
export * from './LogMessageData';
45
export * from './Message';

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './isJson';
2+
export * from './parseError';

src/utils/isJson.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export function isJson(data: string): boolean {
1+
export function isJson(data: unknown): boolean {
22
try {
3-
JSON.parse(data);
3+
JSON.parse(data as string);
44
} catch (error) {
55
return false;
66
}

src/utils/parseError.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function parseError(err: Error | ErrorEvent): object {
2+
return JSON.parse(
3+
JSON.stringify(err, [
4+
'message',
5+
'type',
6+
'name',
7+
'stack',
8+
'fileName',
9+
'lineNumber',
10+
'columnNumber'
11+
])
12+
);
13+
}

0 commit comments

Comments
 (0)