add try catch

This commit is contained in:
Thalles Passos
2025-09-18 10:07:44 -03:00
parent e33bb058a0
commit 0518537868
@@ -48,18 +48,19 @@ export const RailwaySyncFns = {
* If there's a service, triggers a redeploy to pick up the changes. * If there's a service, triggers a redeploy to pick up the changes.
*/ */
async syncSecrets(secretSync: TRailwaySyncWithCredentials, secretMap: TSecretMap) { async syncSecrets(secretSync: TRailwaySyncWithCredentials, secretMap: TSecretMap) {
try {
const { const {
syncOptions: { disableSecretDeletion } syncOptions: { disableSecretDeletion }
} = secretSync; } = secretSync;
const railwaySecrets = await this.getSecrets(secretSync); const railwaySecrets = await this.getSecrets(secretSync);
const config = secretSync.destinationConfig; const config = secretSync.destinationConfig;
const railwaySecretsMap = Object.fromEntries(Object.entries(railwaySecrets).map(([key, secret]) => [key, secret.value])); const railwaySecretsMap = Object.fromEntries(
Object.entries(railwaySecrets).map(([key, secret]) => [key, secret.value])
);
const secretMapMap = Object.fromEntries(Object.entries(secretMap).map(([key, secret]) => [key, secret.value])); const secretMapMap = Object.fromEntries(Object.entries(secretMap).map(([key, secret]) => [key, secret.value]));
const toReplace = disableSecretDeletion const toReplace = disableSecretDeletion ? { ...railwaySecretsMap, ...secretMapMap } : secretMapMap;
? { ...railwaySecretsMap, ...secretMapMap }
: secretMapMap;
const upserted = await RailwayPublicAPI.upsertCollection(secretSync.connection, { const upserted = await RailwayPublicAPI.upsertCollection(secretSync.connection, {
input: { input: {
@@ -68,13 +69,14 @@ export const RailwaySyncFns = {
serviceId: config.serviceId || undefined, serviceId: config.serviceId || undefined,
skipDeploys: true, skipDeploys: true,
variables: toReplace, variables: toReplace,
replace: true, replace: true
} }
}); });
if (!upserted) throw new SecretSyncError({ if (!upserted)
message: "Failed to upsert secrets to Railway", throw new SecretSyncError({
}) message: "Failed to upsert secrets to Railway"
});
if (!config.serviceId) return; if (!config.serviceId) return;
@@ -83,44 +85,66 @@ export const RailwaySyncFns = {
serviceId: config.serviceId, serviceId: config.serviceId,
environmentId: config.environmentId environmentId: config.environmentId
}, },
first: 1, first: 1
}); });
const latestDeploymentId = latestDeployment?.deployments.edges[0].node.id; const latestDeploymentId = latestDeployment?.deployments.edges[0].node.id;
if (!latestDeploymentId) throw new SecretSyncError({ if (!latestDeploymentId)
message: "Failed to get latest deployment from Railway", throw new SecretSyncError({
}) message: "Failed to get latest deployment from Railway"
});
await RailwayPublicAPI.redeployDeployment(secretSync.connection, { await RailwayPublicAPI.redeployDeployment(secretSync.connection, {
input: { input: {
deploymentId: latestDeploymentId deploymentId: latestDeploymentId
} }
}); });
} catch (error) {
if (error instanceof SecretSyncError) throw error;
throw new SecretSyncError({
error,
message: "Failed to sync secrets to Railway"
});
}
}, },
async removeSecrets(secretSync: TRailwaySyncWithCredentials, secretMap: TSecretMap) { async removeSecrets(secretSync: TRailwaySyncWithCredentials, secretMap: TSecretMap) {
const existing = await this.getSecrets(secretSync); const existing = await this.getSecrets(secretSync);
const config = secretSync.destinationConfig; const config = secretSync.destinationConfig;
for await (const secret of Object.keys(existing)) { // Create a new variables object excluding secrets that exist in secretMap
const remainingVariables = Object.fromEntries(
Object.entries(existing)
.filter(([key]) => !(key in secretMap))
.map(([key, secret]) => [key, secret.value])
);
try { try {
if (secret in secretMap) { const upserted = await RailwayPublicAPI.upsertCollection(secretSync.connection, {
await RailwayPublicAPI.deleteVariable(secretSync.connection, {
input: { input: {
projectId: config.projectId, projectId: config.projectId,
environmentId: config.environmentId, environmentId: config.environmentId,
serviceId: config.serviceId || undefined, serviceId: config.serviceId || undefined,
name: secret skipDeploys: true,
variables: remainingVariables,
replace: true
} }
}); });
if (!upserted) {
throw new SecretSyncError({
message: "Failed to remove secrets from Railway"
});
} }
} catch (error) { } catch (error) {
if (error instanceof SecretSyncError) throw error;
throw new SecretSyncError({ throw new SecretSyncError({
error, error,
secretKey: secret message: "Failed to remove secrets from Railway"
}); });
} }
} }
}
}; };