Skip to content

Commit d2fe54f

Browse files
fix: after serverless upgrade, GONE exceptions now throw an error (#160)
1 parent 36354ea commit d2fe54f

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

packages/wallet-service/src/ws/utils.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import {
77
PostToConnectionCommandOutput,
88
DeleteConnectionCommand,
99
DeleteConnectionCommandOutput,
10+
GoneException,
1011
} from '@aws-sdk/client-apigatewaymanagementapi';
1112
import util from 'util';
1213

1314
import { WsConnectionInfo, Severity } from '@src/types';
1415
import { endWsConnection } from '@src/redis';
16+
import createDefaultLogger from '@src/logger';
17+
18+
const logger = createDefaultLogger();
1519

1620
export const connectionInfoFromEvent = (
1721
event: APIGatewayProxyEvent,
@@ -54,16 +58,41 @@ export const sendMessageToClient = async (
5458
endpoint: connInfo.url,
5559
});
5660

61+
const message = JSON.stringify(payload);
62+
5763
const command = new PostToConnectionCommand({
5864
ConnectionId: connInfo.id,
59-
Data: JSON.stringify(payload),
65+
Data: message,
6066
});
6167

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+
)
6796
}
6897
};
6998

0 commit comments

Comments
 (0)