diff --git a/apps/web/pages/api/integrations/[...args].ts b/apps/web/pages/api/integrations/[...args].ts index aed2608820..bb35a435ef 100644 --- a/apps/web/pages/api/integrations/[...args].ts +++ b/apps/web/pages/api/integrations/[...args].ts @@ -5,6 +5,7 @@ import { throwIfNotHaveAdminAccessToTeam } from "@calcom/app-store/_utils/throwI import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; import { deriveAppDictKeyFromType } from "@calcom/lib/deriveAppDictKeyFromType"; import { HttpError } from "@calcom/lib/http-error"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; import type { AppDeclarativeHandler, AppHandler } from "@calcom/types/AppHandler"; @@ -80,13 +81,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { return; } catch (error) { console.error(error); - if (error instanceof HttpError) { - return res.status(error.statusCode).json({ message: error.message }); - } - if (error instanceof Error) { - return res.status(400).json({ message: error.message }); - } - return res.status(404).json({ message: `API handler not found` }); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } }; diff --git a/packages/app-store/alby/api/add.ts b/packages/app-store/alby/api/add.ts index f0f47ff83b..3db09f11db 100644 --- a/packages/app-store/alby/api/add.ts +++ b/packages/app-store/alby/api/add.ts @@ -1,5 +1,6 @@ import type { NextApiRequest, NextApiResponse } from "next"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; import config from "../config.json"; @@ -32,10 +33,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) throw new Error("Unable to create user credential for Alby"); } } catch (error: unknown) { - if (error instanceof Error) { - return res.status(500).json({ message: error.message }); - } - return res.status(500); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } return res.status(200).json({ url: "/apps/alby/setup" }); diff --git a/packages/app-store/giphy/api/add.ts b/packages/app-store/giphy/api/add.ts index ae3e40ee66..5a9bdbc207 100644 --- a/packages/app-store/giphy/api/add.ts +++ b/packages/app-store/giphy/api/add.ts @@ -1,5 +1,6 @@ import type { NextApiRequest, NextApiResponse } from "next"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; import getInstalledAppPath from "../../_utils/getInstalledAppPath"; @@ -44,10 +45,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) throw new Error("Unable to create user credential for giphy"); } } catch (error: unknown) { - if (error instanceof Error) { - return res.status(500).json({ message: error.message }); - } - return res.status(500); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } return res.status(200).json({ url: getInstalledAppPath({ variant: "other", slug: "giphy" }) }); diff --git a/packages/app-store/giphy/api/get.ts b/packages/app-store/giphy/api/get.ts index 7b0d467138..e4099f80d3 100644 --- a/packages/app-store/giphy/api/get.ts +++ b/packages/app-store/giphy/api/get.ts @@ -1,6 +1,8 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { z, ZodError } from "zod"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; + import { GiphyManager } from "../lib"; const giphyUrlRegexp = new RegExp( @@ -35,11 +37,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { const gifImageUrl = await GiphyManager.getGiphyById(giphyId); return res.status(200).json({ image: gifImageUrl }); } catch (error: unknown) { - console.error({ error }); - if (error instanceof Error) { - return res.status(500).json({ message: error.message }); - } - return res.status(500); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } } diff --git a/packages/app-store/giphy/api/search.ts b/packages/app-store/giphy/api/search.ts index f255447bc0..233f464488 100644 --- a/packages/app-store/giphy/api/search.ts +++ b/packages/app-store/giphy/api/search.ts @@ -1,6 +1,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { z, ZodError } from "zod"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; import { GiphyManager } from "../lib"; @@ -39,10 +40,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { nextOffset: total === offset + 1 ? 0 : offset + 1, }); } catch (error: unknown) { - if (error instanceof Error) { - return res.status(500).json({ message: error.message }); - } - return res.status(500); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } } diff --git a/packages/app-store/googlevideo/api/_getAdd.ts b/packages/app-store/googlevideo/api/_getAdd.ts index f310528d02..d6764837e6 100644 --- a/packages/app-store/googlevideo/api/_getAdd.ts +++ b/packages/app-store/googlevideo/api/_getAdd.ts @@ -1,5 +1,6 @@ import type { NextApiRequest, NextApiResponse } from "next"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; import getInstalledAppPath from "../../_utils/getInstalledAppPath"; @@ -32,10 +33,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) throw new Error("Unable to create user credential for google_video"); } } catch (error: unknown) { - if (error instanceof Error) { - return res.status(500).json({ message: error.message }); - } - return res.status(500); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } return res .status(200) diff --git a/packages/app-store/hitpay/api/add.ts b/packages/app-store/hitpay/api/add.ts index b6e9ba123e..e35b15cba5 100644 --- a/packages/app-store/hitpay/api/add.ts +++ b/packages/app-store/hitpay/api/add.ts @@ -1,8 +1,7 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - import { throwIfNotHaveAdminAccessToTeam } from "@calcom/app-store/_utils/throwIfNotHaveAdminAccessToTeam"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; - +import type { NextApiRequest, NextApiResponse } from "next"; import config from "../config.json"; export default async function handler(req: NextApiRequest, res: NextApiResponse) { @@ -37,13 +36,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) }); if (!installation) { - throw new Error("Unable to create user credential for HitPay"); + throw new Error("Unable to create user credential for hitpay"); } } catch (error: unknown) { - const message = - error instanceof Error ? error.message : typeof error === "string" ? error : JSON.stringify(error); - - return res.status(500).json({ message }); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } return res.status(200).json({ url: `/apps/hitpay/setup${teamIdNumber ? `?teamId=${teamIdNumber}` : ""}` }); diff --git a/packages/app-store/jitsivideo/api/add.ts b/packages/app-store/jitsivideo/api/add.ts index 45df37e61d..0d320319c5 100644 --- a/packages/app-store/jitsivideo/api/add.ts +++ b/packages/app-store/jitsivideo/api/add.ts @@ -1,6 +1,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { throwIfNotHaveAdminAccessToTeam } from "@calcom/app-store/_utils/throwIfNotHaveAdminAccessToTeam"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; import getInstalledAppPath from "../../_utils/getInstalledAppPath"; @@ -16,7 +17,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) } const { teamId, returnTo } = req.query; - await throwIfNotHaveAdminAccessToTeam({ teamId: Number(teamId) ?? null, userId: req.session.user.id }); + await throwIfNotHaveAdminAccessToTeam({ + teamId: teamId ? Number(teamId) : null, + userId: req.session.user.id, + }); const installForObject = teamId ? { teamId: Number(teamId) } : { userId: req.session.user.id }; const appType = "jitsi_video"; @@ -42,10 +46,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) throw new Error("Unable to create user credential for jitsivideo"); } } catch (error: unknown) { - if (error instanceof Error) { - return res.status(500).json({ message: error.message }); - } - return res.status(500); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } return res .status(200) diff --git a/packages/app-store/paypal/api/add.ts b/packages/app-store/paypal/api/add.ts index e2762565d0..ac8ef56416 100644 --- a/packages/app-store/paypal/api/add.ts +++ b/packages/app-store/paypal/api/add.ts @@ -1,6 +1,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { throwIfNotHaveAdminAccessToTeam } from "@calcom/app-store/_utils/throwIfNotHaveAdminAccessToTeam"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; import config from "../config.json"; @@ -12,7 +13,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const { teamId } = req.query; - await throwIfNotHaveAdminAccessToTeam({ teamId: Number(teamId) ?? null, userId: req.session.user.id }); + await throwIfNotHaveAdminAccessToTeam({ + teamId: teamId ? Number(teamId) : null, + userId: req.session.user.id, + }); + const installForObject = teamId ? { teamId: Number(teamId) } : { userId: req.session.user.id }; const appType = config.type; @@ -39,10 +44,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) throw new Error("Unable to create user credential for Paypal"); } } catch (error: unknown) { - if (error instanceof Error) { - return res.status(500).json({ message: error.message }); - } - return res.status(500); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } return res.status(200).json({ url: "/apps/paypal/setup" }); diff --git a/packages/app-store/sylapsvideo/api/add.ts b/packages/app-store/sylapsvideo/api/add.ts index edda5533b7..3c49269e8c 100644 --- a/packages/app-store/sylapsvideo/api/add.ts +++ b/packages/app-store/sylapsvideo/api/add.ts @@ -1,5 +1,6 @@ import type { NextApiRequest, NextApiResponse } from "next"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; import getInstalledAppPath from "../../_utils/getInstalledAppPath"; @@ -38,10 +39,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) throw new Error("Unable to create user credential for sylaps"); } } catch (error: unknown) { - if (error instanceof Error) { - return res.status(500).json({ message: error.message }); - } - return res.status(500); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } return res .status(200) diff --git a/packages/app-store/wipemycalother/api/add.ts b/packages/app-store/wipemycalother/api/add.ts index fed23464b4..a6406d313a 100644 --- a/packages/app-store/wipemycalother/api/add.ts +++ b/packages/app-store/wipemycalother/api/add.ts @@ -1,5 +1,6 @@ import type { NextApiRequest, NextApiResponse } from "next"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; import getInstalledAppPath from "../../_utils/getInstalledAppPath"; @@ -36,10 +37,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) throw new Error("Unable to create user credential for wipe-my-cal"); } } catch (error: unknown) { - if (error instanceof Error) { - return res.status(500).json({ message: error.message }); - } - return res.status(500); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } return res.status(200).json({ url: getInstalledAppPath({ variant: "other", slug: "wipe-my-cal" }) }); diff --git a/packages/app-store/wipemycalother/api/wipe.ts b/packages/app-store/wipemycalother/api/wipe.ts index 9a200c3914..8bd45d60e1 100644 --- a/packages/app-store/wipemycalother/api/wipe.ts +++ b/packages/app-store/wipemycalother/api/wipe.ts @@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; import queue from "queue"; import { z, ZodError } from "zod"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma from "@calcom/prisma"; import { BookingStatus } from "@calcom/prisma/enums"; @@ -55,10 +56,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { } await q.start(); } catch (error: unknown) { - if (error instanceof Error) { - return res.status(500).json({ message: error.message }); - } - return res.status(500); + const httpError = getServerErrorFromUnknown(error); + return res.status(httpError.statusCode).json({ message: httpError.message }); } return res.status(200).json({ success: true }); }; diff --git a/packages/app-store/zapier/api/add.ts b/packages/app-store/zapier/api/add.ts index a159b73dd1..8871b61f12 100644 --- a/packages/app-store/zapier/api/add.ts +++ b/packages/app-store/zapier/api/add.ts @@ -1,6 +1,5 @@ import { createDefaultInstallation } from "@calcom/app-store/_utils/installation"; import type { AppDeclarativeHandler } from "@calcom/types/AppHandler"; - import appConfig from "../config.json"; const handler: AppDeclarativeHandler = { @@ -13,8 +12,6 @@ const handler: AppDeclarativeHandler = { newTab: true, url: "https://zapier.com/apps/calcom/integrations", }, - // Creates minimal credential record for tracking purposes only - // No actual credentials are stored since this is a redirect app createCredential: ({ appType, user, slug, teamId }) => createDefaultInstallation({ appType, user: user, slug, key: {}, teamId }), }; diff --git a/packages/features/bookings/lib/handleNewBooking/loadUsers.ts b/packages/features/bookings/lib/handleNewBooking/loadUsers.ts index 804a0c1877..15de5fedf2 100644 --- a/packages/features/bookings/lib/handleNewBooking/loadUsers.ts +++ b/packages/features/bookings/lib/handleNewBooking/loadUsers.ts @@ -1,17 +1,16 @@ import { getOrgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains"; import { - getRoutedUsersWithContactOwnerAndFixedUsers, findMatchingHostsWithEventSegment, getNormalizedHosts, + getRoutedUsersWithContactOwnerAndFixedUsers, } from "@calcom/features/users/lib/getRoutedUsers"; -import { withSelectedCalendars, UserRepository } from "@calcom/features/users/repositories/UserRepository"; -import { HttpError } from "@calcom/lib/http-error"; +import { UserRepository, withSelectedCalendars } from "@calcom/features/users/repositories/UserRepository"; import logger from "@calcom/lib/logger"; import { safeStringify } from "@calcom/lib/safeStringify"; +import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import prisma, { userSelect } from "@calcom/prisma"; -import { Prisma } from "@calcom/prisma/client"; +import type { Prisma } from "@calcom/prisma/client"; import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential"; - import type { NewBookingEventType } from "./getEventTypesFromDB"; const log = logger.getSubLogger({ prefix: ["[loadUsers]:handleNewBooking "] }); @@ -69,10 +68,8 @@ export const loadUsers = async ({ return users; } catch (error) { log.error("Unable to load users", safeStringify(error)); - if (error instanceof HttpError || error instanceof Prisma.PrismaClientKnownRequestError) { - throw new HttpError({ statusCode: 400, message: error.message }); - } - throw new HttpError({ statusCode: 500, message: "Unable to load users" }); + const httpError = getServerErrorFromUnknown(error); + throw httpError; } };