Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Error Handling in setGCPSecret Function #4844

Open
eyeudewj opened this issue Nov 10, 2024 · 0 comments
Open

Enhance Error Handling in setGCPSecret Function #4844

eyeudewj opened this issue Nov 10, 2024 · 0 comments

Comments

@eyeudewj
Copy link

I’ve been looking at the setGCPSecret function, and I think we could make it more robust by enhancing its error handling and logging.

Proposed Changes

Add Try-Catch Blocks:

Wrap the main logic of the function in a try-catch block to catch any errors that occur during the execution of asynchronous operations.

Enhance Logging:

Implement more detailed logging within the catch block to capture and record error details. This will make debugging much easier.

Example Update

export async function setGCPSecret(
  secretName: string,
  secret: string,
  labels: Record<string, string>,
) {
  const fileName = `/tmp/${secretName}.txt`;
  try {
    await writeFile(fileName, secret);

    const exists = await gcpSecretExists(secretName);
    if (!exists) {
      const labelString = Object.keys(labels)
        .map((key) => `${key}=${labels[key]}`)
        .join(',');
      await execCmd(
        `gcloud secrets create ${secretName} --data-file=${fileName} --replication-policy=automatic --labels=${labelString}`,
      );
      debugLog(`Created new GCP secret for ${secretName}`);
    } else {
      await execCmd(
        `gcloud secrets versions add ${secretName} --data-file=${fileName}`,
      );
      debugLog(`Added new version to existing GCP secret for ${secretName}`);
    }
  } catch (error) {
    console.error(`Error setting GCP secret '${secretName}':`, error);
    throw error; // Re-throw after logging
  } finally {
    try {
      await rm(fileName);
    } catch (cleanupError) {
      console.warn(`Issue when trying to remove temporary file '${fileName}':`, cleanupError);
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant