@@ -110,7 +110,45 @@ function mapError(serviceName: string, methodName: string, errResult: any) {
110
110
throw new RpcResponseError ( source , errResult ) ;
111
111
}
112
112
113
- class RpcResponseError {
113
+ const EXCLUDED_META_KEYS = [
114
+ "type" ,
115
+ "code" ,
116
+ "expose" ,
117
+ "message" ,
118
+ "namespace" ,
119
+ "instance" ,
120
+ "source" ,
121
+ ] ;
122
+
123
+ function logfmt ( data : Record < string , any > ) {
124
+ // taken from https://github.com/csquared/node-logfmt/blob/master/lib/stringify.js
125
+ var line = "" ;
126
+
127
+ for ( var key in data ) {
128
+ if ( EXCLUDED_META_KEYS . includes ( key ) ) continue ;
129
+
130
+ var value = data [ key ] ;
131
+ var is_null = false ;
132
+ if ( value == null ) {
133
+ is_null = true ;
134
+ value = "" ;
135
+ } else value = value . toString ( ) ;
136
+
137
+ var needs_quoting = value . indexOf ( " " ) > - 1 || value . indexOf ( "=" ) > - 1 ;
138
+ var needs_escaping = value . indexOf ( '"' ) > - 1 || value . indexOf ( "\\" ) > - 1 ;
139
+
140
+ if ( needs_escaping ) value = value . replace ( / [ " \\ ] / g, "\\$&" ) ;
141
+ if ( needs_quoting || needs_escaping ) value = '"' + value + '"' ;
142
+ if ( value === "" && ! is_null ) value = '""' ;
143
+
144
+ line += key + "=" + value + " " ;
145
+ }
146
+
147
+ // trim trailing space
148
+ return line . substring ( 0 , line . length - 1 ) ;
149
+ }
150
+
151
+ export class RpcResponseError {
114
152
source ?: string [ ] ;
115
153
116
154
constructor ( source : string , responseBody : any ) {
@@ -143,7 +181,9 @@ class RpcResponseError {
143
181
toString ( ) {
144
182
// defineProperty not recognized by typescript, nor is the result of assign recognizable
145
183
const { name, message, instance } = this as any ;
146
- return `${ name } : ${ message } [${ instance } ]` ;
184
+ const meta = logfmt ( this ) ;
185
+
186
+ return `${ name } : ${ message } [${ instance } ]${ meta ? " " + meta : "" } ` ;
147
187
}
148
188
}
149
189
0 commit comments