Skip to content

Commit

Permalink
chore: add catch block after every db transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
harshsbhat committed Oct 9, 2024
1 parent 00213c6 commit be3dc5b
Show file tree
Hide file tree
Showing 23 changed files with 853 additions and 675 deletions.
80 changes: 44 additions & 36 deletions apps/dashboard/lib/trpc/routers/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,46 +50,54 @@ export const createApi = rateLimitedProcedure(ratelimit.create)

const apiId = newId("api");

await db.transaction(async (tx) => {
await tx
.insert(schema.apis)
.values({
id: apiId,
name: input.name,
workspaceId: ws.id,
keyAuthId,
authType: "key",
ipWhitelist: null,
createdAt: new Date(),
})
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We are unable to create the API. Please contact support using support@unkey.dev",
await db
.transaction(async (tx) => {
await tx
.insert(schema.apis)
.values({
id: apiId,
name: input.name,
workspaceId: ws.id,
keyAuthId,
authType: "key",
ipWhitelist: null,
createdAt: new Date(),
})
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We are unable to create the API. Please contact support using support@unkey.dev",
});
});
});

await insertAuditLogs(tx, {
workspaceId: ws.id,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.create",
description: `Created ${apiId}`,
resources: [
{
type: "api",
id: apiId,
await insertAuditLogs(tx, {
workspaceId: ws.id,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.create",
description: `Created ${apiId}`,
resources: [
{
type: "api",
id: apiId,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
});
})
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We are unable to create the API. Please contact support using support@unkey.dev",
});
});
});

return {
id: apiId,
Expand Down
70 changes: 39 additions & 31 deletions apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,46 @@ export const setDefaultApiBytes = rateLimitedProcedure(ratelimit.update)
"We are unable to find the correct keyAuth. Please contact support using support@unkey.dev",
});
}
await db.transaction(async (tx) => {
await tx
.update(schema.keyAuth)
.set({
defaultBytes: input.defaultBytes,
})
.where(eq(schema.keyAuth.id, input.keyAuthId))
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the API default bytes. Please contact support using support@unkey.dev.",
await db
.transaction(async (tx) => {
await tx
.update(schema.keyAuth)
.set({
defaultBytes: input.defaultBytes,
})
.where(eq(schema.keyAuth.id, input.keyAuthId))
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the API default bytes. Please contact support using support@unkey.dev.",
});
});
});
await insertAuditLogs(tx, {
workspaceId: keyAuth.workspaceId,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.update",
description: `Changed ${keyAuth.workspaceId} default byte size for keys from ${keyAuth.defaultBytes} to ${input.defaultBytes}`,
resources: [
{
type: "keyAuth",
id: keyAuth.id,
await insertAuditLogs(tx, {
workspaceId: keyAuth.workspaceId,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.update",
description: `Changed ${keyAuth.workspaceId} default byte size for keys from ${keyAuth.defaultBytes} to ${input.defaultBytes}`,
resources: [
{
type: "keyAuth",
id: keyAuth.id,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
});
})
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were update the default bytes. Please contact support using support@unkey.dev.",
});
});
});
});
70 changes: 39 additions & 31 deletions apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,46 @@ export const setDefaultApiPrefix = rateLimitedProcedure(ratelimit.update)
"We are unable to find the correct keyAuth. Please contact support using support@unkey.dev",
});
}
await db.transaction(async (tx) => {
await tx
.update(schema.keyAuth)
.set({
defaultPrefix: input.defaultPrefix,
})
.where(eq(schema.keyAuth.id, input.keyAuthId))
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the API default prefix. Please contact support using support@unkey.dev.",
await db
.transaction(async (tx) => {
await tx
.update(schema.keyAuth)
.set({
defaultPrefix: input.defaultPrefix,
})
.where(eq(schema.keyAuth.id, input.keyAuthId))
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the API default prefix. Please contact support using support@unkey.dev.",
});
});
});
await insertAuditLogs(tx, {
workspaceId: keyAuth.workspaceId,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.update",
description: `Changed ${keyAuth.workspaceId} default prefix from ${keyAuth.defaultPrefix} to ${input.defaultPrefix}`,
resources: [
{
type: "keyAuth",
id: keyAuth.id,
await insertAuditLogs(tx, {
workspaceId: keyAuth.workspaceId,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.update",
description: `Changed ${keyAuth.workspaceId} default prefix from ${keyAuth.defaultPrefix} to ${input.defaultPrefix}`,
resources: [
{
type: "keyAuth",
id: keyAuth.id,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
});
})
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the default prefix. Please contact support using support@unkey.dev.",
});
});
});
});
78 changes: 43 additions & 35 deletions apps/dashboard/lib/trpc/routers/api/updateDeleteProtection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,51 @@ export const updateAPIDeleteProtection = rateLimitedProcedure(ratelimit.update)
});
}

await db.transaction(async (tx) => {
await tx
.update(schema.apis)
.set({
deleteProtection: input.enabled,
})
.where(eq(schema.apis.id, input.apiId))
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the API. Please contact support using support@unkey.dev.",
await db
.transaction(async (tx) => {
await tx
.update(schema.apis)
.set({
deleteProtection: input.enabled,
})
.where(eq(schema.apis.id, input.apiId))
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the API. Please contact support using support@unkey.dev.",
});
});
});
await insertAuditLogs(tx, {
workspaceId: api.workspace.id,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.update",
description: `API ${api.name} delete protection is now ${
input.enabled ? "enabled" : "disabled"
}.}`,
resources: [
{
type: "api",
id: api.id,
meta: {
deleteProtection: input.enabled,
await insertAuditLogs(tx, {
workspaceId: api.workspace.id,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.update",
description: `API ${api.name} delete protection is now ${
input.enabled ? "enabled" : "disabled"
}.}`,
resources: [
{
type: "api",
id: api.id,
meta: {
deleteProtection: input.enabled,
},
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
});
})
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to update the API. Please contact support using support@unkey.dev",
});
});
});
});
Loading

0 comments on commit be3dc5b

Please sign in to comment.