1
+ import { format } from "util" ;
1
2
import fetch from "node-fetch" ;
2
3
import * as context from "@loke/context" ;
3
4
@@ -110,7 +111,26 @@ function mapError(serviceName: string, methodName: string, errResult: any) {
110
111
throw new RpcResponseError ( source , errResult ) ;
111
112
}
112
113
113
- class RpcResponseError {
114
+ const EXCLUDED_META_KEYS = [
115
+ "type" ,
116
+ "code" ,
117
+ "expose" ,
118
+ "message" ,
119
+ "namespace" ,
120
+ "instance" ,
121
+ "source" ,
122
+ ] ;
123
+
124
+ function metaToString ( meta : Record < string , any > ) {
125
+ if ( ! meta ) return "" ;
126
+
127
+ return Object . keys ( meta )
128
+ . filter ( ( k ) => ! EXCLUDED_META_KEYS . includes ( k ) )
129
+ . map ( ( k ) => format ( "%s=%j" , k , meta [ k ] ) )
130
+ . join ( " " ) ;
131
+ }
132
+
133
+ export class RpcResponseError {
114
134
source ?: string [ ] ;
115
135
116
136
constructor ( source : string , responseBody : any ) {
@@ -121,8 +141,9 @@ class RpcResponseError {
121
141
writable : true ,
122
142
} ) ;
123
143
144
+ // .message .code .type .expose .instance .type are applied here
124
145
Object . assign ( this , responseBody ) ;
125
- if ( ! this . source ) this . source = [ ] ;
146
+
126
147
this . source = [ source , ...( this . source || [ ] ) ] ;
127
148
128
149
Object . defineProperty ( this , "stack" , {
@@ -138,6 +159,14 @@ class RpcResponseError {
138
159
writable : true ,
139
160
} ) ;
140
161
}
162
+
163
+ toString ( ) {
164
+ // defineProperty not recognized by typescript, nor is the result of assign recognizable
165
+ const { name, message, instance } = this as any ;
166
+ const meta = metaToString ( this ) ;
167
+
168
+ return `${ name } : ${ message } [${ instance } ]${ meta ? " " + meta : "" } ` ;
169
+ }
141
170
}
142
171
143
172
export class RPCClient extends BaseClient {
0 commit comments