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

fix: standardize error messaging in all service #920

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/lib/services/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "server-only";

import z from "zod";
import { prisma } from "@formbricks/database";
import { DatabaseError } from "@formbricks/types/v1/errors";
import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/v1/errors";
import { TAction } from "@formbricks/types/v1/actions";
import { ZId } from "@formbricks/types/v1/environment";
import { Prisma } from "@prisma/client";
Expand Down Expand Up @@ -65,7 +65,7 @@ export const createAction = async (data: TJsActionInput) => {
const session = await getSessionCached(sessionId);

if (!session) {
throw new Error("Session not found");
throw new ResourceNotFoundError("Session", sessionId);
}

const actionClass = await getActionClassCached(name, environmentId);
Expand Down
4 changes: 3 additions & 1 deletion packages/lib/services/activity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TActivityFeedItem } from "@formbricks/types/v1/activity";
import { validateInputs } from "../utils/validate";
import { ZId } from "@formbricks/types/v1/environment";
import { cache } from "react";
import { ResourceNotFoundError } from "@formbricks/types/v1/errors";

export const getActivityTimeline = cache(async (personId: string): Promise<TActivityFeedItem[]> => {
validateInputs([personId, ZId]);
Expand Down Expand Up @@ -34,8 +35,9 @@ export const getActivityTimeline = cache(async (personId: string): Promise<TActi
},
},
});

if (!person) {
throw new Error("No such person found");
throw new ResourceNotFoundError("Person", personId);
}
const { attributes, displays, sessions } = person;

Expand Down
4 changes: 3 additions & 1 deletion packages/lib/services/displays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export const createDisplay = async (displayInput: TDisplayInput): Promise<TDispl
export const markDisplayResponded = async (displayId: string): Promise<TDisplay> => {
validateInputs([displayId, ZId]);
try {
if (!displayId) throw new Error("Display ID is required");
if (!displayId) {
throw new ResourceNotFoundError("Display", displayId);
}

const displayPrisma = await prisma.display.update({
where: {
Expand Down
8 changes: 3 additions & 5 deletions packages/lib/services/googleSheet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { prisma } from "@formbricks/database";
import { Prisma } from "@prisma/client";
import { DatabaseError } from "@formbricks/types/v1/errors";
import { DatabaseError, UnknownError } from "@formbricks/types/v1/errors";
import { cache } from "react";
import {
TGoogleCredential,
Expand Down Expand Up @@ -87,8 +87,7 @@ export async function writeData(credentials: TGoogleCredential, spreadsheetId: s
},
(err: Error) => {
if (err) {
throw new Error(`Error while appending data: ${err.message}`);
} else {
throw new UnknownError(`Error while appending data: ${err.message}`);
}
}
);
Expand All @@ -102,8 +101,7 @@ export async function writeData(credentials: TGoogleCredential, spreadsheetId: s
},
(err: Error) => {
if (err) {
throw new Error(`Error while appending data: ${err.message}`);
} else {
throw new UnknownError(`Error while appending data: ${err.message}`);
}
}
);
Expand Down
12 changes: 8 additions & 4 deletions packages/lib/services/membership.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { prisma } from "@formbricks/database";
import { ResourceNotFoundError } from "@formbricks/types/v1/errors";
import { ResourceNotFoundError, DatabaseError } from "@formbricks/types/v1/errors";
import { TMember, TMembership, TMembershipUpdateInput } from "@formbricks/types/v1/memberships";
import { Prisma } from "@prisma/client";
import { cache } from "react";
Expand Down Expand Up @@ -80,9 +80,9 @@ export const updateMembership = async (
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2016") {
throw new ResourceNotFoundError("Membership", `userId: ${userId}, teamId: ${teamId}`);
} else {
throw error; // Re-throw any other errors
}

throw error;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rotimi-best should we maybe also here throw a new UnknownError with the message of the original error? This way we only through our own errors inside the services. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep that's a good idea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, please let me know what you think.

}
};

Expand Down Expand Up @@ -126,6 +126,10 @@ export const transferOwnership = async (currentOwnerId: string, newOwnerId: stri
}),
]);
} catch (error) {
throw new Error("Something went wrong");
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError("Database operation failed");
}

throw error;
}
};
4 changes: 2 additions & 2 deletions packages/lib/services/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "server-only";

import { prisma } from "@formbricks/database";
import { ZId } from "@formbricks/types/v1/environment";
import { DatabaseError } from "@formbricks/types/v1/errors";
import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/v1/errors";
import { TPerson, TPersonUpdateInput } from "@formbricks/types/v1/people";
import { Prisma } from "@prisma/client";
import { revalidateTag, unstable_cache } from "next/cache";
Expand Down Expand Up @@ -249,7 +249,7 @@ export const getOrCreatePersonByUserId = async (userId: string, environmentId: s
const userIdAttributeClass = await getAttributeClassByName(environmentId, "userId");

if (!userIdAttributeClass) {
throw new Error("Attribute class not found for the given environmentId");
throw new ResourceNotFoundError("Attribute class not found for the given environment", environmentId);
}

const personPrisma = await prisma.person.create({
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/services/product.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { prisma } from "@formbricks/database";
import { ZId } from "@formbricks/types/v1/environment";
import { DatabaseError, ValidationError } from "@formbricks/types/v1/errors";
import { DatabaseError, ValidationError, ResourceNotFoundError } from "@formbricks/types/v1/errors";
import type { TProduct, TProductUpdateInput } from "@formbricks/types/v1/product";
import { ZProduct, ZProductUpdateInput } from "@formbricks/types/v1/product";
import { Prisma } from "@prisma/client";
Expand Down Expand Up @@ -221,7 +221,7 @@ export const createProduct = async (environmentId: string, productName: string):
});

if (!environment) {
throw new Error("Invalid environment");
throw new ResourceNotFoundError("Environment", environmentId);
}

const newProduct = await prisma.product.create({
Expand Down
12 changes: 8 additions & 4 deletions packages/lib/services/team.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { prisma } from "@formbricks/database";
import { ZId } from "@formbricks/types/v1/environment";
import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/v1/errors";
import { DatabaseError, ResourceNotFoundError, ValidationError } from "@formbricks/types/v1/errors";
import { TTeam, TTeamUpdateInput } from "@formbricks/types/v1/teams";
import { createId } from "@paralleldrive/cuid2";
import { Prisma } from "@prisma/client";
Expand Down Expand Up @@ -236,7 +236,7 @@ export const createDemoProduct = async (teamId: string) => {

// check if updatedEnvironment exists and it has attributeClasses
if (!updatedEnvironment || !updatedEnvironment.attributeClasses) {
throw new Error("Attribute classes could not be created");
throw new ValidationError("Attribute classes could not be created");
}

const attributeClasses = updatedEnvironment.attributeClasses;
Expand Down Expand Up @@ -323,8 +323,12 @@ export const createDemoProduct = async (teamId: string) => {
})),
}),
]);
} catch (err: any) {
throw new Error(err);
} catch (error: any) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseError("Database operation failed");
}

throw error;
}

// Create a function that creates a survey
Expand Down
9 changes: 9 additions & 0 deletions packages/types/v1/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class ValidationError extends Error {
}
}

class UnknownError extends Error {
statusCode = 500;
constructor(message: string) {
super(message);
this.name = "DatabaseError";
}
}

class DatabaseError extends Error {
statusCode = 500;
constructor(message: string) {
Expand Down Expand Up @@ -83,6 +91,7 @@ export {
ValidationError,
DatabaseError,
UniqueConstraintError,
UnknownError,
ForeignKeyConstraintError,
OperationNotAllowedError,
AuthenticationError,
Expand Down