@@ -62,7 +62,7 @@ export class PriceServiceConnection {
62
62
private wsClient : undefined | ResilientWebSocket ;
63
63
private wsEndpoint : undefined | string ;
64
64
65
- private logger : undefined | Logger ;
65
+ private logger : Logger ;
66
66
67
67
private priceFeedRequestConfig : PriceFeedRequestConfig ;
68
68
@@ -96,9 +96,30 @@ export class PriceServiceConnection {
96
96
97
97
this . priceFeedCallbacks = new Map ( ) ;
98
98
99
- this . logger = config ?. logger ;
99
+ // Default logger is console for only warnings and errors.
100
+ this . logger = config ?. logger || {
101
+ trace : ( ) => { } ,
102
+ debug : ( ) => { } ,
103
+ info : ( ) => { } ,
104
+ warn : console . warn ,
105
+ error : console . error ,
106
+ } ;
107
+
100
108
this . onWsError = ( error : Error ) => {
101
- this . logger ?. error ( error ) ;
109
+ this . logger . error ( error ) ;
110
+
111
+ // Exit the process if it is running in node.
112
+ if (
113
+ typeof process !== "undefined" &&
114
+ typeof process . exit === "function"
115
+ ) {
116
+ this . logger . error ( "Halting the process due to the websocket error" ) ;
117
+ process . exit ( 1 ) ;
118
+ } else {
119
+ this . logger . error (
120
+ "Cannot halt process. Please handle the websocket error."
121
+ ) ;
122
+ }
102
123
} ;
103
124
104
125
this . wsEndpoint = makeWebsocketUrl ( endpoint ) ;
@@ -333,28 +354,28 @@ export class PriceServiceConnection {
333
354
binary : this . priceFeedRequestConfig . binary ,
334
355
} ;
335
356
336
- this . logger ? .info ( "Resubscribing to existing price feeds." ) ;
357
+ this . logger . info ( "Resubscribing to existing price feeds." ) ;
337
358
this . wsClient ?. send ( JSON . stringify ( message ) ) ;
338
359
}
339
360
} ;
340
361
341
362
this . wsClient . onMessage = ( data : WebSocket . Data ) => {
342
- this . logger ? .info ( `Received message ${ data . toString ( ) } ` ) ;
363
+ this . logger . info ( `Received message ${ data . toString ( ) } ` ) ;
343
364
344
365
let message : ServerMessage ;
345
366
346
367
try {
347
368
message = JSON . parse ( data . toString ( ) ) as ServerMessage ;
348
369
} catch ( e : any ) {
349
- this . logger ? .error ( `Error parsing message ${ data . toString ( ) } as JSON.` ) ;
350
- this . logger ? .error ( e ) ;
370
+ this . logger . error ( `Error parsing message ${ data . toString ( ) } as JSON.` ) ;
371
+ this . logger . error ( e ) ;
351
372
this . onWsError ( e ) ;
352
373
return ;
353
374
}
354
375
355
376
if ( message . type === "response" ) {
356
377
if ( message . status === "error" ) {
357
- this . logger ? .error (
378
+ this . logger . error (
358
379
`Error response from the websocket server ${ message . error } .`
359
380
) ;
360
381
this . onWsError ( new Error ( message . error ) ) ;
@@ -364,10 +385,10 @@ export class PriceServiceConnection {
364
385
try {
365
386
priceFeed = PriceFeed . fromJson ( message . price_feed ) ;
366
387
} catch ( e : any ) {
367
- this . logger ? .error (
388
+ this . logger . error (
368
389
`Error parsing price feeds from message ${ data . toString ( ) } .`
369
390
) ;
370
- this . logger ? .error ( e ) ;
391
+ this . logger . error ( e ) ;
371
392
this . onWsError ( e ) ;
372
393
return ;
373
394
}
@@ -378,7 +399,7 @@ export class PriceServiceConnection {
378
399
}
379
400
}
380
401
} else {
381
- this . logger ? .warn (
402
+ this . logger . warn (
382
403
`Ignoring unsupported server response ${ data . toString ( ) } .`
383
404
) ;
384
405
}
0 commit comments