mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-22 19:39:01 -05:00
fix: adds try...catch in all services (#2494)
Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
This commit is contained in:
@@ -18,15 +18,19 @@ export const canUserAccessAttributeClass = async (
|
||||
validateInputs([userId, ZId], [attributeClassId, ZId]);
|
||||
if (!userId) return false;
|
||||
|
||||
const attributeClass = await getAttributeClass(attributeClassId);
|
||||
if (!attributeClass) return false;
|
||||
try {
|
||||
const attributeClass = await getAttributeClass(attributeClassId);
|
||||
if (!attributeClass) return false;
|
||||
|
||||
const hasAccessToEnvironment = await hasUserEnvironmentAccess(userId, attributeClass.environmentId);
|
||||
if (!hasAccessToEnvironment) return false;
|
||||
const hasAccessToEnvironment = await hasUserEnvironmentAccess(userId, attributeClass.environmentId);
|
||||
if (!hasAccessToEnvironment) return false;
|
||||
|
||||
return true;
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
[`users-${userId}-attributeClasses-${attributeClassId}`],
|
||||
[`canUserAccessAttributeClass-${userId}-${attributeClassId}`],
|
||||
{ revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [`attributeClasses-${attributeClassId}`] }
|
||||
)();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import "server-only";
|
||||
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { unstable_cache } from "next/cache";
|
||||
|
||||
import { prisma } from "@formbricks/database";
|
||||
@@ -28,13 +29,18 @@ export const getAttributeClass = async (attributeClassId: string): Promise<TAttr
|
||||
validateInputs([attributeClassId, ZId]);
|
||||
|
||||
try {
|
||||
return await prisma.attributeClass.findFirst({
|
||||
const attributeClass = await prisma.attributeClass.findFirst({
|
||||
where: {
|
||||
id: attributeClassId,
|
||||
},
|
||||
});
|
||||
|
||||
return attributeClass;
|
||||
} catch (error) {
|
||||
throw new DatabaseError(`Database error when fetching attributeClass with id ${attributeClassId}`);
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
throw new DatabaseError(error.message);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[`getAttributeClass-${attributeClassId}`],
|
||||
@@ -75,9 +81,10 @@ export const getAttributeClasses = async (
|
||||
return true;
|
||||
});
|
||||
} catch (error) {
|
||||
throw new DatabaseError(
|
||||
`Database error when fetching attributeClasses for environment ${environmentId}`
|
||||
);
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
throw new DatabaseError(error.message);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[`getAttributeClasses-${environmentId}-${page}`],
|
||||
@@ -114,7 +121,10 @@ export const updateAttributeClass = async (
|
||||
|
||||
return attributeClass;
|
||||
} catch (error) {
|
||||
throw new DatabaseError(`Database error when updating attribute class with id ${attributeClassId}`);
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
throw new DatabaseError(error.message);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -123,14 +133,21 @@ export const getAttributeClassByName = async (environmentId: string, name: strin
|
||||
async (): Promise<TAttributeClass | null> => {
|
||||
validateInputs([environmentId, ZId], [name, ZString]);
|
||||
|
||||
const attributeClass = await prisma.attributeClass.findFirst({
|
||||
where: {
|
||||
environmentId,
|
||||
name,
|
||||
},
|
||||
});
|
||||
try {
|
||||
const attributeClass = await prisma.attributeClass.findFirst({
|
||||
where: {
|
||||
environmentId,
|
||||
name,
|
||||
},
|
||||
});
|
||||
|
||||
return attributeClass;
|
||||
return attributeClass;
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
throw new DatabaseError(error.message);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[`getAttributeClassByName-${environmentId}-${name}`],
|
||||
{
|
||||
@@ -148,25 +165,32 @@ export const createAttributeClass = async (
|
||||
): Promise<TAttributeClass | null> => {
|
||||
validateInputs([environmentId, ZId], [name, ZString], [type, ZAttributeClassType]);
|
||||
|
||||
const attributeClass = await prisma.attributeClass.create({
|
||||
data: {
|
||||
name,
|
||||
type,
|
||||
environment: {
|
||||
connect: {
|
||||
id: environmentId,
|
||||
try {
|
||||
const attributeClass = await prisma.attributeClass.create({
|
||||
data: {
|
||||
name,
|
||||
type,
|
||||
environment: {
|
||||
connect: {
|
||||
id: environmentId,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
attributeClassCache.revalidate({
|
||||
id: attributeClass.id,
|
||||
environmentId: attributeClass.environmentId,
|
||||
name: attributeClass.name,
|
||||
});
|
||||
attributeClassCache.revalidate({
|
||||
id: attributeClass.id,
|
||||
environmentId: attributeClass.environmentId,
|
||||
name: attributeClass.name,
|
||||
});
|
||||
|
||||
return attributeClass;
|
||||
return attributeClass;
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
throw new DatabaseError(error.message);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteAttributeClass = async (attributeClassId: string): Promise<TAttributeClass> => {
|
||||
@@ -187,6 +211,9 @@ export const deleteAttributeClass = async (attributeClassId: string): Promise<TA
|
||||
|
||||
return deletedAttributeClass;
|
||||
} catch (error) {
|
||||
throw new DatabaseError(`Database error when deleting webhook with ID ${attributeClassId}`);
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
throw new DatabaseError(error.message);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user