@@ -6,6 +6,99 @@ function readNonEmptyString(value: unknown): string | undefined {
66 return typeof value === 'string' && value . trim ( ) !== '' ? value : undefined ;
77}
88
9+ const LITERAL_ESCAPE_PATTERN = / \\ (?: [ n r t b f v 0 ' " \\ ] | u [ 0 - 9 a - f A - F ] { 4 } | x [ 0 - 9 a - f A - F ] { 2 } ) / ; // literal escape sequences like \n, \t, \b, \f, \v, \uXXXX, \xHH
10+ const REAL_CONTROL_PATTERN = / [ \n \t \b \f \v ] / ; // real control characters like \n, \t, \b, \f, \v
11+
12+ function decodeLiteralEscapes ( text : string ) : string {
13+ let out = '' ;
14+ for ( let i = 0 ; i < text . length ; i += 1 ) {
15+ const char = text [ i ] ;
16+ if ( char !== '\\' || i + 1 >= text . length ) {
17+ out += char ;
18+ continue ;
19+ }
20+
21+ const next = text [ i + 1 ] ;
22+ switch ( next ) {
23+ case 'n' :
24+ out += '\n' ;
25+ i += 1 ;
26+ break ;
27+ case 't' :
28+ out += '\t' ;
29+ i += 1 ;
30+ break ;
31+ case 'r' :
32+ out += '\r' ;
33+ i += 1 ;
34+ break ;
35+ case 'b' :
36+ out += '\b' ;
37+ i += 1 ;
38+ break ;
39+ case 'f' :
40+ out += '\f' ;
41+ i += 1 ;
42+ break ;
43+ case 'v' :
44+ out += '\v' ;
45+ i += 1 ;
46+ break ;
47+ case '0' :
48+ out += '\0' ;
49+ i += 1 ;
50+ break ;
51+ case '\\' :
52+ out += '\\' ;
53+ i += 1 ;
54+ break ;
55+ case '"' :
56+ out += '"' ;
57+ i += 1 ;
58+ break ;
59+ case "'" :
60+ out += "'" ;
61+ i += 1 ;
62+ break ;
63+ case 'u' : {
64+ const hex = text . slice ( i + 2 , i + 6 ) ;
65+ if ( / ^ [ 0 - 9 a - f A - F ] { 4 } $ / . test ( hex ) ) {
66+ out += String . fromCharCode ( Number . parseInt ( hex , 16 ) ) ;
67+ i += 5 ;
68+ } else {
69+ out += char ;
70+ }
71+ break ;
72+ }
73+ case 'x' : {
74+ const hex = text . slice ( i + 2 , i + 4 ) ;
75+ if ( / ^ [ 0 - 9 a - f A - F ] { 2 } $ / . test ( hex ) ) {
76+ out += String . fromCharCode ( Number . parseInt ( hex , 16 ) ) ;
77+ i += 3 ;
78+ } else {
79+ out += char ;
80+ }
81+ break ;
82+ }
83+ default :
84+ out += char ;
85+ break ;
86+ }
87+ }
88+ return out ;
89+ }
90+
91+ // Decodes JSON/C-style escape sequences (e.g., \n, \t) in error messages, preserving real control characters and normalizing line endings.
92+ export function decodeErrorMessageEscapes ( text : string ) : string {
93+ const normalized = text . replace ( / \r \n / g, '\n' ) . replace ( / \r / g, '\n' ) ;
94+ if ( REAL_CONTROL_PATTERN . test ( normalized ) || ! LITERAL_ESCAPE_PATTERN . test ( normalized ) ) {
95+ // If the string contains real control characters or no literal escape sequences, return the normalized string.
96+ return normalized ;
97+ }
98+ // else decode the literal escape sequences and normalize line endings
99+ return decodeLiteralEscapes ( normalized ) . replace ( / \r \n / g, '\n' ) . replace ( / \r / g, '\n' ) ;
100+ }
101+
9102/**
10103 * Reads a display string from API-shaped errors: `message`, then nested
11104 * `error.message`. When neither exists, stringifies the value.
@@ -36,30 +129,33 @@ function stringifyError(error: unknown): string {
36129 * User-facing error text. Prefers HTTP body `{ error: { message } }` when
37130 * present (e.g. TrueForgeError), then `error.message` / `error.error.message`,
38131 * then `fallback`, then a string form of the value.
132+ *
133+ * Escape sequences (`\n`, `\t`, `\r`, `\uXXXX`, …) are decoded for display when
134+ * still present as literal text.
39135 */
40136export function getErrorMessage ( error : unknown , fallback ?: string ) : string {
41137 if ( typeof error === 'string' ) {
42138 const trimmed = error . trim ( ) ;
43- if ( trimmed !== '' ) return error ;
44- return fallback ?? error ;
139+ if ( trimmed !== '' ) return decodeErrorMessageEscapes ( error ) ;
140+ return fallback != null ? decodeErrorMessageEscapes ( fallback ) : decodeErrorMessageEscapes ( error ) ;
45141 }
46142
47143 if ( isRecord ( error ) && 'body' in error ) {
48144 const fromBody = messageFromErrorShaped ( error . body ) ;
49- if ( fromBody != null ) return fromBody ;
145+ if ( fromBody != null ) return decodeErrorMessageEscapes ( fromBody ) ;
50146 if ( error . body != null ) {
51147 if ( typeof error . body === 'string' ) {
52148 const trimmed = error . body . trim ( ) ;
53- if ( trimmed !== '' ) return error . body ;
149+ if ( trimmed !== '' ) return decodeErrorMessageEscapes ( error . body ) ;
54150 } else {
55151 return stringifyError ( error . body ) ;
56152 }
57153 }
58154 }
59155
60156 const fromShape = messageFromErrorShaped ( error ) ;
61- if ( fromShape != null ) return fromShape ;
157+ if ( fromShape != null ) return decodeErrorMessageEscapes ( fromShape ) ;
62158
63- if ( fallback != null ) return fallback ;
159+ if ( fallback != null ) return decodeErrorMessageEscapes ( fallback ) ;
64160 return stringifyError ( error ) ;
65161}
0 commit comments