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 all 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
83 changes: 45 additions & 38 deletions apps/dashboard/lib/trpc/routers/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const createApi = rateLimitedProcedure(ratelimit.create)
.catch((_err) => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "We are unable to create an API. Please contact support using support@unkey.dev",
message: "We are unable to create an API. Please try again or contact support@unkey.dev",
});
});
if (!ws) {
Expand All @@ -44,52 +44,59 @@ export const createApi = rateLimitedProcedure(ratelimit.create)
} catch (_err) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "We are unable to create an API. Please contact support using support@unkey.dev",
message: "We are unable to create an API. Please try again or contact support@unkey.dev",
});
}

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 try again or contact 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 try again or contact support@unkey.dev",
});
});
});

return {
id: apiId,
Expand Down
6 changes: 3 additions & 3 deletions apps/dashboard/lib/trpc/routers/api/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export const deleteApi = rateLimitedProcedure(ratelimit.delete)
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"We are unable to delete this API. Please contact support using support@unkey.dev",
"We are unable to delete this API. Please try again or contact support@unkey.dev",
});
});
if (!api || api.workspace.tenantId !== ctx.tenant.id) {
throw new TRPCError({
code: "NOT_FOUND",
message: "The API does not exist. Please contact support using support@unkey.dev",
message: "The API does not exist. Please try again or contact support@unkey.dev",
});
}
if (api.deleteProtection) {
Expand Down Expand Up @@ -107,7 +107,7 @@ export const deleteApi = rateLimitedProcedure(ratelimit.delete)
} catch (_err) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "We are unable to delete the API. Please contact support using support@unkey.dev",
message: "We are unable to delete the API. Please try again or contact support@unkey.dev",
});
}
});
74 changes: 41 additions & 33 deletions apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,56 @@ 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) {
throw new TRPCError({
code: "NOT_FOUND",
message:
"We are unable to find the correct keyAuth. Please contact support using support@unkey.dev",
"We are unable to find the correct keyAuth. Please try again or contact 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.",
});
});
});
});
75 changes: 41 additions & 34 deletions apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,56 @@ 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) {
throw new TRPCError({
code: "NOT_FOUND",
message:
"We are unable to find the correct keyAuth. Please contact support using support@unkey.dev",
"We are unable to find the correct keyAuth. Please try again or contact 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.",
});
});
});
});
Loading
Loading