Details: We have a Lambda function that is attempting to connect to a postgres RDS database. If there is a subsequent request, the execution environment should attempt to reuse the existing database connection. There are some cases where the connection seems to timeout but the exception is not handled properly resulting in the Lambda to exit with status 128.
Error:
{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "Error: Connection timeout",
}
Code:
let dataSource: DataSource;
export async function initializeDataSource(): Promise<DataSource> {
if (dataSource) {
if (!dataSource.isInitialized) return await tryToInitializeDataSource();
return dataSource;
}
const dataSourceOptions = await getDataSourceOptions();
dataSource = new DataSource({
...dataSourceOptions,
type: 'postgres',
database: 'identity',
logging: ['error'],
synchronize: false,
entities: registeredEntities,
});
return await tryToInitializeDataSource();
}
async function tryToInitializeDataSource(): Promise<DataSource> {
try {
dataSource = await dataSource.initialize();
return dataSource;
} catch (error) {
console.log(error);
throw e;
}
}
Question: Any ideas why this is being thrown as an unhandled promise and is not being handled properly?