Skip to content

Commit

Permalink
fix: Better PG error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
storm1729 committed Jan 3, 2024
1 parent 5786e81 commit 7848c7d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/app/api/v0/check_email/checkUserInDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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">;
Expand Down Expand Up @@ -40,7 +41,7 @@ export async function checkUserInDB(req: NextRequest): Promise<UserWithSub> {
.select("*")
.eq("api_token", token);
if (error) {
throw error;
throw convertPgError(error);
}
if (!data?.length) {
throw newEarlyResponse(
Expand All @@ -62,7 +63,7 @@ export async function checkUserInDB(req: NextRequest): Promise<UserWithSub> {
.limit(1)
.single();
if (res2.error) {
throw res2.error;
throw convertPgError(res2.error);
}

const subAndCalls = res2.data;
Expand Down
8 changes: 7 additions & 1 deletion src/app/api/v0/check_email/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +26,12 @@ export async function POST(req: NextRequest): Promise<Response> {
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`);

Expand Down Expand Up @@ -89,7 +95,7 @@ export async function POST(req: NextRequest): Promise<Response> {
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
)
Expand Down
13 changes: 12 additions & 1 deletion src/app/api/v1/bulk/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -17,6 +17,17 @@ interface BulkPayload {

export const POST = async (req: NextRequest): Promise<Response> => {
try {
if (!ENABLE_BULK) {
return Response.json(
{
error: "Bulk verification is not enabled",
},
{
status: 403,
}
);
}

const cookieStore = cookies();
const supabase = createClient(cookieStore);
const {
Expand Down
7 changes: 7 additions & 0 deletions src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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;

1 comment on commit 7848c7d

@vercel
Copy link

@vercel vercel bot commented on 7848c7d Jan 3, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.