Skip to content

Commit

Permalink
fix: standardize error messaging in all service (formbricks#920)
Browse files Browse the repository at this point in the history
* fix: standardize error messaging in all service

* fix: use unknownerror
  • Loading branch information
rotimi-best committed Oct 4, 2023
1 parent 812d411 commit 0ec3248
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 19 deletions.
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 @@ -92,7 +92,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
Expand Up @@ -2,7 +2,7 @@ import "server-only";

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 @@ -89,8 +89,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 @@ -104,8 +103,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
13 changes: 9 additions & 4 deletions packages/lib/services/membership.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "server-only";

import { prisma } from "@formbricks/database";
import { ResourceNotFoundError } from "@formbricks/types/v1/errors";
import { ResourceNotFoundError, DatabaseError, UnknownError } 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 @@ -102,9 +102,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;
}
};

Expand Down Expand Up @@ -148,6 +148,11 @@ 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");
}

const message = error instanceof Error ? error.message : "";
throw new UnknownError(`Error while transfering ownership: ${message}`);
}
};
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 @@ -250,7 +250,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
12 changes: 8 additions & 4 deletions packages/lib/services/team.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, 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 @@ -245,7 +245,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 @@ -332,8 +332,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

0 comments on commit 0ec3248

Please sign in to comment.