Skip to content

Commit e4266df

Browse files
committed
fix: add try-catch blocks to transactions
1 parent ebd69c6 commit e4266df

File tree

3 files changed

+65
-45
lines changed

3 files changed

+65
-45
lines changed

src/actions/build-image.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import deleteBuildImage from "~/operations/deleteBuildImage";
66

77
export async function destroy(id: string, _: FormData) {
88
const serviceId = await db.transaction(async tx => {
9-
await removeImage(id);
10-
const image = await deleteBuildImage(tx, id);
11-
if (!image) throw new Error("Invalid Build Image");
12-
return image.serviceId;
9+
try {
10+
await removeImage(id);
11+
const image = await deleteBuildImage(tx, id);
12+
if (!image) throw new Error("Invalid Build Image");
13+
return image.serviceId;
14+
} catch (error) {
15+
console.error(error);
16+
throw error;
17+
}
1318
});
1419
redirect(`/services/${serviceId}`);
1520
}

src/actions/service-networks.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import deleteNetwork from "~/operations/deleteNetwork";
88

99
export async function create(serviceId: string) {
1010
await db.transaction(async tx => {
11-
const { id: networkId } = await createNetwork(tx, serviceId, "custom");
12-
await createDockerNetwork(networkId);
11+
try {
12+
const { id: networkId } = await createNetwork(tx, serviceId, "custom");
13+
await createDockerNetwork(networkId);
14+
} catch (error) {
15+
console.error(error);
16+
throw error;
17+
}
1318
});
1419
redirect(`/services/${serviceId}/networks`);
1520
}

src/actions/service.ts

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,48 @@ import { mergeObjects } from "~/utils/object";
2424
export async function create(formData: FormData) {
2525
const data = NewServiceSchema.parse(getData(formData));
2626
await db.transaction(async tx => {
27-
const service = await createService(tx, data);
28-
if (data.kind === "database") {
29-
const envEntries: [string, string][] = [];
30-
const portEntries: [string, string][] = [];
31-
if (data.repo === "postgres") {
32-
envEntries.push(["POSTGRES_PASSWORD", crypto.randomUUID()]);
33-
// portEntries.push(["5432", "5432"]);
34-
const volume = await createVolume(tx, { containerPath: "/var/lib/postgresql/data", serviceId: service.id });
35-
await createDockerVolume(volume.id);
36-
}
37-
if (data.repo === "mysql") {
38-
envEntries.push(["MYSQL_ROOT_PASSWORD", "example"]);
39-
portEntries.push(["3306", "3306"]);
40-
const volume = await createVolume(tx, { containerPath: "/var/lib/mysql", serviceId: service.id });
41-
await createDockerVolume(volume.id);
42-
}
43-
if (data.repo === "mongo") {
44-
envEntries.push(["MONGO_INITDB_ROOT_USERNAME", "root"]);
45-
envEntries.push(["MONGO_INITDB_ROOT_PASSWORD", "example"]);
46-
portEntries.push(["27017", "27017"]);
47-
const volume = await createVolume(tx, { containerPath: "/data/db", serviceId: service.id });
48-
await createDockerVolume(volume.id);
49-
}
50-
if (data.repo === "redis") {
51-
envEntries.push(["REDIS_ARGS", "--appendonly yes"]);
52-
// portEntries.push(["6379", "6379"]);
53-
const volume = await createVolume(tx, { containerPath: "/data", serviceId: service.id });
54-
await createDockerVolume(volume.id);
55-
}
56-
if (envEntries.length) {
57-
await syncEnvironmentVariables(tx, service.id, Object.fromEntries(envEntries));
58-
}
59-
if (portEntries.length) {
60-
await syncPorts(tx, service.id, Object.fromEntries(portEntries));
27+
try {
28+
const service = await createService(tx, data);
29+
if (data.kind === "database") {
30+
const envEntries: [string, string][] = [];
31+
const portEntries: [string, string][] = [];
32+
if (data.repo === "postgres") {
33+
envEntries.push(["POSTGRES_PASSWORD", crypto.randomUUID()]);
34+
// portEntries.push(["5432", "5432"]);
35+
const volume = await createVolume(tx, { containerPath: "/var/lib/postgresql/data", serviceId: service.id });
36+
await createDockerVolume(volume.id);
37+
}
38+
if (data.repo === "mysql") {
39+
envEntries.push(["MYSQL_ROOT_PASSWORD", "example"]);
40+
portEntries.push(["3306", "3306"]);
41+
const volume = await createVolume(tx, { containerPath: "/var/lib/mysql", serviceId: service.id });
42+
await createDockerVolume(volume.id);
43+
}
44+
if (data.repo === "mongo") {
45+
envEntries.push(["MONGO_INITDB_ROOT_USERNAME", "root"]);
46+
envEntries.push(["MONGO_INITDB_ROOT_PASSWORD", "example"]);
47+
portEntries.push(["27017", "27017"]);
48+
const volume = await createVolume(tx, { containerPath: "/data/db", serviceId: service.id });
49+
await createDockerVolume(volume.id);
50+
}
51+
if (data.repo === "redis") {
52+
envEntries.push(["REDIS_ARGS", "--appendonly yes"]);
53+
// portEntries.push(["6379", "6379"]);
54+
const volume = await createVolume(tx, { containerPath: "/data", serviceId: service.id });
55+
await createDockerVolume(volume.id);
56+
}
57+
if (envEntries.length) {
58+
await syncEnvironmentVariables(tx, service.id, Object.fromEntries(envEntries));
59+
}
60+
if (portEntries.length) {
61+
await syncPorts(tx, service.id, Object.fromEntries(portEntries));
62+
}
63+
const network = await createNetwork(tx, service.id, "provider");
64+
await createDockerNetwork(network.id);
6165
}
62-
const network = await createNetwork(tx, service.id, "provider");
63-
await createDockerNetwork(network.id);
66+
} catch (error) {
67+
console.error(error);
68+
throw error;
6469
}
6570
});
6671
redirect("/services");
@@ -81,9 +86,14 @@ export async function update(id: string, _: unknown, formData: FormData): Promis
8186
patch.status = "idle";
8287
}
8388
await db.transaction(async tx => {
84-
await updateService(tx, id, patch);
85-
await syncEnvironmentVariables(tx, id, env);
86-
await syncPorts(tx, id, ports);
89+
try {
90+
await updateService(tx, id, patch);
91+
await syncEnvironmentVariables(tx, id, env);
92+
await syncPorts(tx, id, ports);
93+
} catch (error) {
94+
console.error(error);
95+
throw error;
96+
}
8797
});
8898
redirect("/services");
8999
}

0 commit comments

Comments
 (0)