1- import type * as Sentry from "@sentry/node" ;
21import { createHash } from "crypto" ;
32
3+ import { getSentryEventId } from "./sentry.js" ;
44import { ConsumerMethodPath , ServerError , ServerErrorsItem } from "./types.js" ;
55
66const MAX_MSG_LENGTH = 2048 ;
@@ -10,13 +10,11 @@ export default class ServerErrorCounter {
1010 private errorCounts : Map < string , number > ;
1111 private errorDetails : Map < string , ConsumerMethodPath & ServerError > ;
1212 private sentryEventIds : Map < string , string > ;
13- private sentry : typeof Sentry | undefined ;
1413
1514 constructor ( ) {
1615 this . errorCounts = new Map ( ) ;
1716 this . errorDetails = new Map ( ) ;
1817 this . sentryEventIds = new Map ( ) ;
19- this . tryImportSentry ( ) ;
2018 }
2119
2220 public addServerError ( serverError : ConsumerMethodPath & ServerError ) {
@@ -25,7 +23,11 @@ export default class ServerErrorCounter {
2523 this . errorDetails . set ( key , serverError ) ;
2624 }
2725 this . errorCounts . set ( key , ( this . errorCounts . get ( key ) || 0 ) + 1 ) ;
28- this . captureSentryEventId ( key ) ;
26+
27+ const sentryEventId = getSentryEventId ( ) ;
28+ if ( sentryEventId ) {
29+ this . sentryEventIds . set ( key , sentryEventId ) ;
30+ }
2931 }
3032
3133 public getAndResetServerErrors ( ) {
@@ -38,8 +40,8 @@ export default class ServerErrorCounter {
3840 method : serverError . method ,
3941 path : serverError . path ,
4042 type : serverError . type ,
41- msg : this . getTruncatedMessage ( serverError . msg ) ,
42- traceback : this . getTruncatedStack ( serverError . traceback ) ,
43+ msg : truncateExceptionMessage ( serverError . msg ) ,
44+ traceback : truncateExceptionStackTrace ( serverError . traceback ) ,
4345 sentry_event_id : this . sentryEventIds . get ( key ) || null ,
4446 error_count : count ,
4547 } ) ;
@@ -61,48 +63,30 @@ export default class ServerErrorCounter {
6163 ] . join ( "|" ) ;
6264 return createHash ( "md5" ) . update ( hashInput ) . digest ( "hex" ) ;
6365 }
66+ }
6467
65- private getTruncatedMessage ( msg : string ) {
66- msg = msg . trim ( ) ;
67- if ( msg . length <= MAX_MSG_LENGTH ) {
68- return msg ;
69- }
70- const suffix = "... (truncated)" ;
71- const cutoff = MAX_MSG_LENGTH - suffix . length ;
72- return msg . substring ( 0 , cutoff ) + suffix ;
73- }
74-
75- private getTruncatedStack ( stack : string ) {
76- const suffix = "... (truncated) ..." ;
77- const cutoff = MAX_STACKTRACE_LENGTH - suffix . length ;
78- const lines = stack . trim ( ) . split ( "\n" ) ;
79- const truncatedLines : string [ ] = [ ] ;
80- let length = 0 ;
81- for ( const line of lines ) {
82- if ( length + line . length + 1 > cutoff ) {
83- truncatedLines . push ( suffix ) ;
84- break ;
85- }
86- truncatedLines . push ( line ) ;
87- length += line . length + 1 ;
88- }
89- return truncatedLines . join ( "\n" ) ;
90- }
91-
92- private captureSentryEventId ( serverErrorKey : string ) {
93- if ( this . sentry && this . sentry . lastEventId ) {
94- const eventId = this . sentry . lastEventId ( ) ;
95- if ( eventId ) {
96- this . sentryEventIds . set ( serverErrorKey , eventId ) ;
97- }
98- }
68+ export function truncateExceptionMessage ( msg : string ) {
69+ if ( msg . length <= MAX_MSG_LENGTH ) {
70+ return msg ;
9971 }
72+ const suffix = "... (truncated)" ;
73+ const cutoff = MAX_MSG_LENGTH - suffix . length ;
74+ return msg . substring ( 0 , cutoff ) + suffix ;
75+ }
10076
101- private async tryImportSentry ( ) {
102- try {
103- this . sentry = await import ( "@sentry/node" ) ;
104- } catch ( e ) {
105- // Sentry SDK is not installed, ignore
77+ export function truncateExceptionStackTrace ( stack : string ) {
78+ const suffix = "... (truncated) ..." ;
79+ const cutoff = MAX_STACKTRACE_LENGTH - suffix . length ;
80+ const lines = stack . trim ( ) . split ( "\n" ) ;
81+ const truncatedLines : string [ ] = [ ] ;
82+ let length = 0 ;
83+ for ( const line of lines ) {
84+ if ( length + line . length + 1 > cutoff ) {
85+ truncatedLines . push ( suffix ) ;
86+ break ;
10687 }
88+ truncatedLines . push ( line ) ;
89+ length += line . length + 1 ;
10790 }
91+ return truncatedLines . join ( "\n" ) ;
10892}
0 commit comments