diff --git a/src/app/api/v0/check_email/checkUserInDb.ts b/src/app/api/v0/check_email/checkUserInDb.ts index 0e4bee3..786aece 100644 --- a/src/app/api/v0/check_email/checkUserInDb.ts +++ b/src/app/api/v0/check_email/checkUserInDb.ts @@ -5,6 +5,7 @@ import { CheckEmailOutput } from "@reacherhq/api"; import { Tables } from "@/supabase/database.types"; import { NextRequest } from "next/server"; import { supabaseAdmin } from "@/supabase/supabaseAdmin"; +import { convertPgError } from "@/util/helpers"; type UserWithSub = { user: Tables<"users">; @@ -40,7 +41,7 @@ export async function checkUserInDB(req: NextRequest): Promise { .select("*") .eq("api_token", token); if (error) { - throw error; + throw convertPgError(error); } if (!data?.length) { throw newEarlyResponse( @@ -62,7 +63,7 @@ export async function checkUserInDB(req: NextRequest): Promise { .limit(1) .single(); if (res2.error) { - throw res2.error; + throw convertPgError(res2.error); } const subAndCalls = res2.data; diff --git a/src/app/api/v0/check_email/route.ts b/src/app/api/v0/check_email/route.ts index a716319..865c5b9 100644 --- a/src/app/api/v0/check_email/route.ts +++ b/src/app/api/v0/check_email/route.ts @@ -12,6 +12,7 @@ import { updateSendinblue } from "@/util/sendinblue"; import { sentryException } from "@/util/sentry"; import { supabaseAdmin } from "@/supabase/supabaseAdmin"; import { NextRequest } from "next/server"; +import * as Sentry from "@sentry/nextjs"; // https://vercel.com/changelog/serverless-functions-can-now-run-up-to-5-minutes export const maxDuration = 300; // 5min @@ -25,7 +26,12 @@ export async function POST(req: NextRequest): Promise { console.log("[🐢] POST /v0/check_email"); try { + Sentry.setTag("rch.route", "/v0/check_email"); const { user, rateLimitHeaders } = await checkUserInDB(req); + Sentry.setContext("user", { + supbaseUuid: user.id, + }); + const d1 = performance.now() - startTime; console.log(`[🐢] checkUserInDB: ${Math.round(d1)}ms`); @@ -89,7 +95,7 @@ export async function POST(req: NextRequest): Promise { newEarlyResponse( Response.json( { - error: `${response.error.message}: ${response.error.details}`, + error: `Postgres error: [${response.error.code}] ${response.error.message}: ${response.error.details} ${response.error.hint}`, }, response ) diff --git a/src/app/api/v1/bulk/route.ts b/src/app/api/v1/bulk/route.ts index 5c34ff0..e72e7bc 100644 --- a/src/app/api/v1/bulk/route.ts +++ b/src/app/api/v1/bulk/route.ts @@ -2,7 +2,7 @@ import { NextRequest } from "next/server"; import amqplib from "amqplib"; import { supabaseAdmin } from "@/supabase/supabaseAdmin"; import { sentryException } from "@/util/sentry"; -import { getWebappURL } from "@/util/helpers"; +import { ENABLE_BULK, getWebappURL } from "@/util/helpers"; import { isEarlyResponse } from "@/app/api/v0/check_email/checkUserInDb"; import { SAAS_100K_PRODUCT_ID, getApiUsage } from "@/util/subs"; import { Json, Tables } from "@/supabase/database.types"; @@ -17,6 +17,17 @@ interface BulkPayload { export const POST = async (req: NextRequest): Promise => { try { + if (!ENABLE_BULK) { + return Response.json( + { + error: "Bulk verification is not enabled", + }, + { + status: 403, + } + ); + } + const cookieStore = cookies(); const supabase = createClient(cookieStore); const { diff --git a/src/util/helpers.ts b/src/util/helpers.ts index b2f741f..71633e2 100644 --- a/src/util/helpers.ts +++ b/src/util/helpers.ts @@ -2,6 +2,7 @@ import axios, { AxiosError, AxiosResponse } from "axios"; import retry from "async-retry"; import { format, parseISO } from "date-fns"; import { enUS, fr } from "date-fns/locale"; +import { PostgrestError } from "@supabase/supabase-js"; // Gets the currently depoloyed URL. export const getWebappURL = (): string => { @@ -106,4 +107,10 @@ export function formatDate(d: string | Date, locale?: string): string { }); } +export function convertPgError(err: PostgrestError): Error { + return new Error( + `PostgrestError: [${err.code}] ${err.message} ${err.details} ${err.hint}` + ); +} + export const ENABLE_BULK: boolean = false;