66 type A11yDOMState ,
77} from "@/context-providers/a11y-dom" ;
88import type { FrameChunkEvent } from "@/context-providers/a11y-dom/types" ;
9+ import { formatUnknownError } from "@/utils" ;
910import { waitForSettledDOM } from "@/utils/waitForSettledDOM" ;
1011
1112const DOM_CAPTURE_MAX_ATTEMPTS = 3 ;
@@ -26,6 +27,34 @@ export interface CaptureDOMOptions {
2627}
2728
2829const MAX_DOM_CAPTURE_RETRIES = 10 ;
30+ const MAX_DOM_CAPTURE_DIAGNOSTIC_CHARS = 400 ;
31+
32+ function sanitizeDomCaptureText ( value : string ) : string {
33+ if ( value . length === 0 ) {
34+ return value ;
35+ }
36+ const withoutControlChars = Array . from ( value , ( char ) => {
37+ const code = char . charCodeAt ( 0 ) ;
38+ return ( code >= 0 && code < 32 ) || code === 127 ? " " : char ;
39+ } ) . join ( "" ) ;
40+ return withoutControlChars . replace ( / \s + / g, " " ) . trim ( ) ;
41+ }
42+
43+ function truncateDomCaptureDiagnostic ( value : string ) : string {
44+ if ( value . length <= MAX_DOM_CAPTURE_DIAGNOSTIC_CHARS ) {
45+ return value ;
46+ }
47+ const omittedChars = value . length - MAX_DOM_CAPTURE_DIAGNOSTIC_CHARS ;
48+ return `${ value . slice ( 0 , MAX_DOM_CAPTURE_DIAGNOSTIC_CHARS ) } ... [truncated ${ omittedChars } chars]` ;
49+ }
50+
51+ function formatDomCaptureDiagnostic ( value : unknown ) : string {
52+ const normalized = sanitizeDomCaptureText ( formatUnknownError ( value ) ) ;
53+ if ( normalized . length === 0 ) {
54+ return "unknown callback error" ;
55+ }
56+ return truncateDomCaptureDiagnostic ( normalized ) ;
57+ }
2958
3059class DomChunkAggregator {
3160 private parts : string [ ] = [ ] ;
@@ -139,9 +168,9 @@ export async function captureDOMState(
139168 } catch ( error ) {
140169 if ( debug ) {
141170 console . warn (
142- `[DOM] onFrameChunk callback failed: ${
143- error instanceof Error ? error . message : "unknown callback error"
144- } `
171+ `[DOM] onFrameChunk callback failed: ${ formatDomCaptureDiagnostic (
172+ error
173+ ) } `
145174 ) ;
146175 }
147176 }
0 commit comments