From bb658a0758c886a751f0798df7ce805a3fe911c4 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 14 Aug 2023 12:37:30 +0200 Subject: [PATCH] fix(build): more resilient S3 handling for JSON feed MONGOSH-1242 (#1570) Account for `NoSuchKey` errors being thrown when downloading files from AWS. --- packages/build/src/download-center/config.ts | 35 ++++++++++++++------ packages/build/src/index.ts | 2 ++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/packages/build/src/download-center/config.ts b/packages/build/src/download-center/config.ts index 4ba6317ec..9ee9cf57a 100644 --- a/packages/build/src/download-center/config.ts +++ b/packages/build/src/download-center/config.ts @@ -45,9 +45,15 @@ export async function createAndPublishDownloadCenterConfig( accessKeyId: awsAccessKeyId, secretAccessKey: awsSecretAccessKey, }); - const existingDownloadCenterConfig = await dlcenter.downloadConfig( - CONFIGURATION_KEY - ); + let existingDownloadCenterConfig: DownloadCenterConfig | undefined; + try { + existingDownloadCenterConfig = await dlcenter.downloadConfig( + CONFIGURATION_KEY + ); + } catch (err: any) { + console.warn('Failed to get existing download center config', err); + if (err?.code !== 'NoSuchKey') throw err; + } const getVersionConfig = () => createVersionConfig(packageInformation, publicArtifactBaseUrl); @@ -59,7 +65,7 @@ export async function createAndPublishDownloadCenterConfig( : createDownloadCenterConfig(getVersionConfig); console.warn('Created download center config:'); - //console.dir(config, { depth: Infinity }); + console.dir(config, { depth: Infinity }); validateConfigSchema(config); @@ -69,13 +75,20 @@ export async function createAndPublishDownloadCenterConfig( secretAccessKey: awsSecretAccessKey, }); const jsonFeedArtifactkey = `${ARTIFACTS_FOLDER}/mongosh.json`; - const existingJsonFeedText = await dlcenterArtifacts.downloadAsset( - jsonFeedArtifactkey - ); - const existingJsonFeed = existingJsonFeedText + let existingJsonFeedText; + try { + existingJsonFeedText = await dlcenterArtifacts.downloadAsset( + jsonFeedArtifactkey + ); + } catch (err: any) { + console.warn('Failed to get existing JSON feed text', err); + if (err?.code !== 'NoSuchKey') throw err; + } + + const existingJsonFeed: JsonFeed | undefined = existingJsonFeedText ? JSON.parse(existingJsonFeedText.toString()) : undefined; - const injectedJsonFeed = injectedJsonFeedFile + const injectedJsonFeed: JsonFeed | undefined = injectedJsonFeedFile ? JSON.parse(await fs.readFile(injectedJsonFeedFile, 'utf8')) : undefined; const currentJsonFeedEntry = await createJsonFeedEntry( @@ -86,7 +99,7 @@ export async function createAndPublishDownloadCenterConfig( versions: [currentJsonFeedEntry], }; console.warn('Adding new JSON feed entry:'); - //console.dir(currentJsonFeedEntry, { depth: Infinity }); + console.dir(currentJsonFeedEntry, { depth: Infinity }); const newJsonFeed = mergeFeeds( existingJsonFeed, @@ -227,7 +240,7 @@ export async function createJsonFeedEntry( }; } -function mergeFeeds(...args: JsonFeed[]): JsonFeed { +function mergeFeeds(...args: (JsonFeed | undefined)[]): JsonFeed { const newFeed: JsonFeed = { versions: [], }; diff --git a/packages/build/src/index.ts b/packages/build/src/index.ts index e4f0cbcdd..92d7aef27 100644 --- a/packages/build/src/index.ts +++ b/packages/build/src/index.ts @@ -10,6 +10,8 @@ import type { Config, PackageVariant } from './config'; export { getArtifactUrl, downloadMongoDb }; if (require.main === module) { + Error.stackTraceLimit = 200; + (async () => { const command = process.argv[2]; if (