@@ -7,11 +7,15 @@ import {
7
7
PostToConnectionCommandOutput ,
8
8
DeleteConnectionCommand ,
9
9
DeleteConnectionCommandOutput ,
10
+ GoneException ,
10
11
} from '@aws-sdk/client-apigatewaymanagementapi' ;
11
12
import util from 'util' ;
12
13
13
14
import { WsConnectionInfo , Severity } from '@src/types' ;
14
15
import { endWsConnection } from '@src/redis' ;
16
+ import createDefaultLogger from '@src/logger' ;
17
+
18
+ const logger = createDefaultLogger ( ) ;
15
19
16
20
export const connectionInfoFromEvent = (
17
21
event : APIGatewayProxyEvent ,
@@ -54,16 +58,41 @@ export const sendMessageToClient = async (
54
58
endpoint : connInfo . url ,
55
59
} ) ;
56
60
61
+ const message = JSON . stringify ( payload ) ;
62
+
57
63
const command = new PostToConnectionCommand ( {
58
64
ConnectionId : connInfo . id ,
59
- Data : JSON . stringify ( payload ) ,
65
+ Data : message ,
60
66
} ) ;
61
67
62
- const response : PostToConnectionCommandOutput = await apiGwClient . send ( command ) ;
63
- // http GONE(410) means client is disconnected, but still exists on our connection store
64
- if ( response . $metadata . httpStatusCode === 410 ) {
65
- // cleanup connection and subscriptions from redis if GONE
66
- return endWsConnection ( client , connInfo . id ) ;
68
+ try {
69
+ const response : PostToConnectionCommandOutput = await apiGwClient . send ( command ) ;
70
+
71
+ if ( response . $metadata . httpStatusCode !== 200 ) {
72
+ logger . error ( response . $metadata ) ;
73
+ throw new Error ( `Status code from post to connection is not 200: ${ response . $metadata . httpStatusCode } ` ) ;
74
+ }
75
+ } catch ( e ) {
76
+ if ( e instanceof GoneException ) {
77
+ logger . debug ( `Received GONE exception, closing ${ connInfo . id } ` ) ;
78
+ return endWsConnection ( client , connInfo . id ) ;
79
+ }
80
+
81
+ logger . error ( e ) ;
82
+
83
+ // Unhandled exception. We shouldn't end the connection as it might be a temporary
84
+ // instability with api gateway.
85
+ //
86
+ // Alert and move on, no need to throw here
87
+ addAlert (
88
+ 'Unhandled error while sending websocket message to client' ,
89
+ 'The wallet-service was unable to handle an error while attempting to send a message to a websocket client. Please check the logs.' ,
90
+ Severity . MINOR ,
91
+ {
92
+ ConnectionId : connInfo . id ,
93
+ Message : message ,
94
+ } ,
95
+ )
67
96
}
68
97
} ;
69
98
0 commit comments