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

chore: add catch block after every db transaction #2287

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
72 changes: 40 additions & 32 deletions apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const setDefaultApiBytes = rateLimitedProcedure(ratelimit.update)
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to find the KeyAuth. Please contact support using support@unkey.dev.",
"We were unable to find the KeyAuth. Please try again or contact support@unkey.dev.",
});
});
if (!keyAuth || keyAuth.workspaceId !== input.workspaceId) {
Expand All @@ -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 try again or contact 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 unable to update the default bytes. Please try again or contact support@unkey.dev.",
});
});
});
});
73 changes: 40 additions & 33 deletions apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export const setDefaultApiPrefix = rateLimitedProcedure(ratelimit.update)
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We were unable to find KeyAuth. Please contact support using support@unkey.dev.",
message: "We were unable to find KeyAuth. Please try again or contact support@unkey.dev.",
});
});
if (!keyAuth || keyAuth.workspaceId !== input.workspaceId) {
Expand All @@ -32,38 +31,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 try again or contact 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 try again or contact support@unkey.dev.",
});
});
});
});
80 changes: 44 additions & 36 deletions apps/dashboard/lib/trpc/routers/api/updateDeleteProtection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,55 @@ export const updateAPIDeleteProtection = rateLimitedProcedure(ratelimit.update)
throw new TRPCError({
code: "NOT_FOUND",
message:
"We are unable to find the correct API. Please contact support using support@unkey.dev.",
"We are unable to find the correct API. Please try again or contact 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 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 try again or contact 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
Loading