File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -39,4 +39,26 @@ describe("client-js", () => {
3939 c . request ( "foo" , [ ] ) ;
4040 } ) ;
4141
42+ it ( "can send a request and error" , ( ) => {
43+ const transport = new EventEmitterTransport ( "foo://unique-uri" ) ;
44+ const c = new RequestManager ( [ transport ] ) ;
45+ transport . onData = ( fn ) => {
46+ transport . connection . on ( "message" , ( ) => {
47+ fn ( JSON . stringify ( {
48+ jsonrpc : "2.0" ,
49+ id : 1 ,
50+ error : {
51+ code : 0 ,
52+ message : "out of order" ,
53+ data : {
54+ foo : "bar" ,
55+ } ,
56+ } ,
57+ } ) ) ;
58+ } ) ;
59+ } ;
60+ c . connect ( ) ;
61+ expect ( c . request ( "foo" , [ ] ) ) . rejects . toThrow ( ) ;
62+ } ) ;
63+
4264} ) ;
Original file line number Diff line number Diff line change @@ -33,7 +33,10 @@ class RequestManager {
3333 const i = id ++ ;
3434 // naively grab first transport and use it
3535 const transport = this . transports [ 0 ] ;
36- this . requests [ i ] = resolve ;
36+ this . requests [ i ] = {
37+ resolve,
38+ reject,
39+ } ;
3740 transport . sendData ( JSON . stringify ( {
3841 jsonrpc : "2.0" ,
3942 id : i ,
@@ -49,12 +52,23 @@ class RequestManager {
4952 }
5053 private onData ( data : string ) : void {
5154 const parsedData = JSON . parse ( data ) ;
52- if ( typeof parsedData . result === "undefined" ) {
55+ if ( typeof parsedData . result === "undefined" && typeof parsedData . error === "undefined" ) {
5356 return ;
5457 }
58+ const req = this . requests [ parsedData . id ] ;
5559 // call request callback for id
56- if ( this . requests [ parsedData . id ] ) {
57- this . requests [ parsedData . id ] ( parsedData ) ;
60+ if ( req ) {
61+ if ( parsedData . error ) {
62+ req . reject ( new Error (
63+ [
64+ `code: ${ parsedData . error . code } ` ,
65+ `message: ${ parsedData . error . message } ` ,
66+ `data: ${ JSON . stringify ( parsedData . error . data ) } ` ,
67+ ] . join ( "\n" ) ,
68+ ) ) ;
69+ } else {
70+ req . resolve ( parsedData ) ;
71+ }
5872 delete this . requests [ parsedData . id ] ;
5973 }
6074 }
Original file line number Diff line number Diff line change @@ -10,7 +10,6 @@ describe("client-js", () => {
1010
1111 it ( "has a request method that returns a promise" , ( ) => {
1212 const c = new Client ( new RequestManager ( [ new EventEmitterTransport ( "foo://unique" ) ] ) ) ;
13- console . log ( "test city before" ) ; // tslint:disable-line
1413 expect ( typeof c . request ) . toEqual ( "function" ) ;
1514 expect ( typeof c . request ( "my_method" , null ) . then ) . toEqual ( "function" ) ;
1615 } ) ;
You can’t perform that action at this time.
0 commit comments