@@ -62,7 +62,7 @@ export class PriceServiceConnection {
6262 private wsClient : undefined | ResilientWebSocket ;
6363 private wsEndpoint : undefined | string ;
6464
65- private logger : undefined | Logger ;
65+ private logger : Logger ;
6666
6767 private priceFeedRequestConfig : PriceFeedRequestConfig ;
6868
@@ -96,9 +96,30 @@ export class PriceServiceConnection {
9696
9797 this . priceFeedCallbacks = new Map ( ) ;
9898
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+
100108 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+ }
102123 } ;
103124
104125 this . wsEndpoint = makeWebsocketUrl ( endpoint ) ;
@@ -333,28 +354,28 @@ export class PriceServiceConnection {
333354 binary : this . priceFeedRequestConfig . binary ,
334355 } ;
335356
336- this . logger ? .info ( "Resubscribing to existing price feeds." ) ;
357+ this . logger . info ( "Resubscribing to existing price feeds." ) ;
337358 this . wsClient ?. send ( JSON . stringify ( message ) ) ;
338359 }
339360 } ;
340361
341362 this . wsClient . onMessage = ( data : WebSocket . Data ) => {
342- this . logger ? .info ( `Received message ${ data . toString ( ) } ` ) ;
363+ this . logger . info ( `Received message ${ data . toString ( ) } ` ) ;
343364
344365 let message : ServerMessage ;
345366
346367 try {
347368 message = JSON . parse ( data . toString ( ) ) as ServerMessage ;
348369 } 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 ) ;
351372 this . onWsError ( e ) ;
352373 return ;
353374 }
354375
355376 if ( message . type === "response" ) {
356377 if ( message . status === "error" ) {
357- this . logger ? .error (
378+ this . logger . error (
358379 `Error response from the websocket server ${ message . error } .`
359380 ) ;
360381 this . onWsError ( new Error ( message . error ) ) ;
@@ -364,10 +385,10 @@ export class PriceServiceConnection {
364385 try {
365386 priceFeed = PriceFeed . fromJson ( message . price_feed ) ;
366387 } catch ( e : any ) {
367- this . logger ? .error (
388+ this . logger . error (
368389 `Error parsing price feeds from message ${ data . toString ( ) } .`
369390 ) ;
370- this . logger ? .error ( e ) ;
391+ this . logger . error ( e ) ;
371392 this . onWsError ( e ) ;
372393 return ;
373394 }
@@ -378,7 +399,7 @@ export class PriceServiceConnection {
378399 }
379400 }
380401 } else {
381- this . logger ? .warn (
402+ this . logger . warn (
382403 `Ignoring unsupported server response ${ data . toString ( ) } .`
383404 ) ;
384405 }
0 commit comments