Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

attach partnerId to Axiom error logs in withPartnerProfile

Kiran K80b0190f2ed1
diff --git a/apps/web/lib/api/errors.ts b/apps/web/lib/api/errors.ts
index e3d0fbeee3..0f129910fd 100644
--- a/apps/web/lib/api/errors.ts
+++ b/apps/web/lib/api/errors.ts
@@ -4,7 +4,7 @@ import { generateErrorMessage } from "zod-error";
import { ZodOpenApiResponseObject } from "zod-openapi";
import * as z from "zod/v4";
import { logger } from "../axiom/server";
-import { WorkspaceProps } from "../types";
+import { PartnerProps, WorkspaceProps } from "../types";
import { ErrorCode, ErrorCodes } from "./error-codes";
const speakeasyErrorOverrides: Record<z.infer<typeof ErrorCode>, string> = {
@@ -93,9 +93,11 @@ export function fromZodError(error: z.ZodError): ErrorResponse {
export function handleApiError({
error,
workspace,
+ partner,
}: {
error: any;
workspace?: Pick<WorkspaceProps, "id">;
+ partner?: Pick<PartnerProps, "id">;
}): ErrorResponse & { status: number } {
console.error(error.message);
@@ -103,6 +105,7 @@ export function handleApiError({
logger.error(error.message, {
error,
...(workspace?.id ? { workspaceId: workspace.id } : {}),
+ ...(partner?.id ? { partnerId: partner.id } : {}),
});
after(() => logger.flush());
diff --git a/apps/web/lib/auth/partner.ts b/apps/web/lib/auth/partner.ts
index 5ad3856247..8f1d6b8f9c 100644
--- a/apps/web/lib/auth/partner.ts
+++ b/apps/web/lib/auth/partner.ts
@@ -1,4 +1,4 @@
-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { DubApiError, handleApiError } from "@/lib/api/errors";
import { withAxiom } from "@/lib/axiom/server";
import { prisma } from "@/lib/prisma";
import { PartnerBetaFeatures, PartnerProps } from "@/lib/types";
@@ -12,6 +12,7 @@ import {
import { PartnerUser } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { headers } from "next/headers";
+import { NextResponse } from "next/server";
import { getPartnerFeatureFlags } from "../edge-config";
import { ratelimit } from "../upstash";
import { partnerPlatformSchema } from "../zod/schemas/partners";
@@ -73,6 +74,7 @@ export const withPartnerProfile = (
let apiKey: string | undefined;
let requestHeaders = await headers();
let responseHeaders = new Headers();
+ let partner: Pick<PartnerProps, "id"> | undefined;
try {
// Restrict access to the network program
@@ -195,6 +197,7 @@ export const withPartnerProfile = (
}
const { defaultPartnerId, id: userId } = session.user;
+
if (!defaultPartnerId) {
throw new DubApiError({
code: "not_found",
@@ -202,6 +205,10 @@ export const withPartnerProfile = (
});
}
+ partner = {
+ id: defaultPartnerId,
+ };
+
// Check API rate limit
const url = new URL(req.url || "", PARTNERS_DOMAIN);
const isAnalytics =
@@ -281,7 +288,7 @@ export const withPartnerProfile = (
preferredEarningStructures,
salesChannels,
platforms,
- ...partner
+ ...partnerProps
} = partnerUser.partner;
return await handler({
@@ -290,7 +297,7 @@ export const withPartnerProfile = (
searchParams,
session,
partner: {
- ...flattenVeriffMetadata(partner),
+ ...flattenVeriffMetadata(partnerProps),
industryInterests: industryInterests.map(
({ industryInterest }) => industryInterest,
),
@@ -308,8 +315,16 @@ export const withPartnerProfile = (
},
headers: responseHeaders,
});
- } catch (error) {
- return handleAndReturnErrorResponse(error, responseHeaders);
+ } catch (err) {
+ const { error, status } = handleApiError({
+ error: err,
+ partner,
+ });
+
+ return NextResponse.json(
+ { error },
+ { headers: responseHeaders, status },
+ );
}
},
);