async function checkFallbackImage()

in source/custom-resource/index.ts [466:503]


async function checkFallbackImage(requestProperties: CheckFallbackImageRequestProperties): Promise<{ Message: string; Data: unknown }> {
  const { FallbackImageS3Bucket, FallbackImageS3Key } = requestProperties as CheckFallbackImageRequestProperties;

  if (isNullOrWhiteSpace(FallbackImageS3Bucket)) {
    throw new CustomResourceError('S3BucketNotProvided', 'You need to provide the default fallback image bucket.');
  }

  if (isNullOrWhiteSpace(FallbackImageS3Key)) {
    throw new CustomResourceError('S3KeyNotProvided', 'You need to provide the default fallback image object key.');
  }

  let data = {};

  for (let retry = 1; retry <= RETRY_COUNT; retry++) {
    try {
      data = await s3Client.headObject({ Bucket: FallbackImageS3Bucket, Key: FallbackImageS3Key }).promise();
      break;
    } catch (error) {
      if (retry === RETRY_COUNT || ![ErrorCodes.ACCESS_DENIED, ErrorCodes.FORBIDDEN].includes(error.code)) {
        console.error(`Either the object does not exist or you don't have permission to access the object: ${FallbackImageS3Bucket}/${FallbackImageS3Key}`);

        throw new CustomResourceError(
          'FallbackImageError',
          `Either the object does not exist or you don't have permission to access the object: ${FallbackImageS3Bucket}/${FallbackImageS3Key}`
        );
      } else {
        console.info('Waiting for retry...');

        await sleep(getRetryTimeout(retry));
      }
    }
  }

  return {
    Message: 'The default fallback image validated.',
    Data: data
  };
}