Skip to content

Commit 8bc7b1a

Browse files
committed
fix: move stringify reason method to utils
1 parent 27f5d92 commit 8bc7b1a

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

packages/javascript/src/addons/consoleCatcher.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import type { ConsoleLogEvent } from '@hawk.so/types';
55
import Sanitizer from '../modules/sanitizer';
6+
import { stringifyRejectionReason } from 'src/utils/event';
67

78
/**
89
* Maximum number of console logs to store
@@ -189,25 +190,6 @@ export class ConsoleCatcher {
189190
this.consoleOutput.push(logEvent);
190191
}
191192

192-
/**
193-
* Converts a promise rejection reason to a string message.
194-
*
195-
* String(obj) gives "[object Object]" and JSON.stringify("str")
196-
* adds unwanted quotes.
197-
*
198-
* @param reason - The rejection reason from PromiseRejectionEvent
199-
*/
200-
private stringifyReason(reason: unknown): string {
201-
if (reason instanceof Error) {
202-
return reason.message;
203-
}
204-
if (typeof reason === 'string') {
205-
return reason;
206-
}
207-
208-
return JSON.stringify(Sanitizer.sanitize(reason));
209-
}
210-
211193
/**
212194
* Creates a console log event from an error or promise rejection
213195
*
@@ -231,7 +213,7 @@ export class ConsoleCatcher {
231213
method: 'error',
232214
timestamp: new Date(),
233215
type: 'UnhandledRejection',
234-
message: this.stringifyReason(event.reason),
216+
message: stringifyRejectionReason(event.reason),
235217
stack: event.reason?.stack || '',
236218
fileLine: '',
237219
};

packages/javascript/src/utils/event.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,22 @@ export function getErrorFromEvent(event: ErrorEvent | PromiseRejectionEvent): Er
8080

8181
return Sanitizer.sanitize(error);
8282
}
83+
84+
/**
85+
* Converts a promise rejection reason to a string message.
86+
*
87+
* String(obj) gives "[object Object]" and JSON.stringify("str")
88+
* adds unwanted quotes.
89+
*
90+
* @param reason - The rejection reason from PromiseRejectionEvent
91+
*/
92+
export function stringifyRejectionReason(reason: unknown): string {
93+
if (reason instanceof Error) {
94+
return reason.message;
95+
}
96+
if (typeof reason === 'string') {
97+
return reason;
98+
}
99+
100+
return JSON.stringify(Sanitizer.sanitize(reason));
101+
}

0 commit comments

Comments
 (0)