Skip to content

Commit

Permalink
fix: after serverless upgrade, GONE exceptions now throw an error (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabadesso committed Apr 30, 2024
1 parent c19c2c3 commit efcab3b
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions packages/wallet-service/src/ws/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PostToConnectionCommandOutput,
DeleteConnectionCommand,
DeleteConnectionCommandOutput,
GoneException,
} from '@aws-sdk/client-apigatewaymanagementapi';
import { Logger } from 'winston';
import createDefaultLogger from '@src/logger';
Expand All @@ -15,6 +16,9 @@ import util from 'util';
import { Severity } from '@wallet-service/common/src/types';
import { WsConnectionInfo } from '@src/types';
import { endWsConnection } from '@src/redis';
import createDefaultLogger from '@src/logger';

const logger = createDefaultLogger();

export const connectionInfoFromEvent = (
event: APIGatewayProxyEvent,
Expand Down Expand Up @@ -60,16 +64,41 @@ export const sendMessageToClient = async (
endpoint: connInfo.url,
});

const message = JSON.stringify(payload);

const command = new PostToConnectionCommand({
ConnectionId: connInfo.id,
Data: JSON.stringify(payload),
Data: message,
});

const response: PostToConnectionCommandOutput = await apiGwClient.send(command);
// http GONE(410) means client is disconnected, but still exists on our connection store
if (response.$metadata.httpStatusCode === 410) {
// cleanup connection and subscriptions from redis if GONE
return endWsConnection(client, connInfo.id);
try {
const response: PostToConnectionCommandOutput = await apiGwClient.send(command);

if (response.$metadata.httpStatusCode !== 200) {
logger.error(response.$metadata);
throw new Error(`Status code from post to connection is not 200: ${response.$metadata.httpStatusCode}`);
}
} catch (e) {
if (e instanceof GoneException) {
logger.debug(`Received GONE exception, closing ${connInfo.id}`);
return endWsConnection(client, connInfo.id);
}

logger.error(e);

// Unhandled exception. We shouldn't end the connection as it might be a temporary
// instability with api gateway.
//
// Alert and move on, no need to throw here
addAlert(
'Unhandled error while sending websocket message to client',
'The wallet-service was unable to handle an error while attempting to send a message to a websocket client. Please check the logs.',
Severity.MINOR,
{
ConnectionId: connInfo.id,
Message: message,
},
)
}
};

Expand Down

0 comments on commit efcab3b

Please sign in to comment.