Skip to content

Commit b656a4c

Browse files
committed
fix(trust-portal): enhance error handling for domain ownership checks
1 parent bcdb150 commit b656a4c

1 file changed

Lines changed: 61 additions & 41 deletions

File tree

apps/app/src/app/(app)/[orgId]/trust/portal-settings/actions/custom-domain.ts

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -124,52 +124,72 @@ export const customDomainAction = authActionClient
124124

125125
// Check for 409 domain_already_in_use - domain exists on our project with pending verification
126126
if (vercelError.statusCode === 409 && vercelError.body) {
127+
// Parse error body separately to avoid catching db/revalidation errors
128+
let errorBody: { error?: { code?: string; projectId?: string; domain?: { verified?: boolean; verification?: Array<{ value?: string }> } } } | null = null;
127129
try {
128-
const errorBody = JSON.parse(vercelError.body);
129-
const errorData = errorBody?.error;
130-
131-
if (
132-
errorData?.code === 'domain_already_in_use' &&
133-
errorData?.projectId === env.TRUST_PORTAL_PROJECT_ID
134-
) {
135-
// Domain already exists on our project - extract verification info and save it
136-
const domainInfo = errorData.domain;
137-
const vercelVerification = domainInfo?.verification?.[0]?.value || null;
138-
const isVercelDomain = domainInfo?.verified === false;
139-
140-
console.log(
141-
`Domain ${domain} already exists on project, extracting verification info:`,
142-
vercelVerification,
143-
);
144-
145-
await db.trust.upsert({
146-
where: { organizationId: activeOrganizationId },
147-
update: {
148-
domain,
149-
domainVerified: false,
150-
isVercelDomain,
151-
vercelVerification,
152-
},
153-
create: {
154-
organizationId: activeOrganizationId,
155-
domain,
156-
domainVerified: false,
157-
isVercelDomain,
158-
vercelVerification,
159-
},
160-
});
161-
162-
revalidatePath(`/${activeOrganizationId}/trust`);
163-
revalidatePath(`/${activeOrganizationId}/trust/portal-settings`);
164-
revalidateTag(`organization_${activeOrganizationId}`, 'max');
130+
errorBody = JSON.parse(vercelError.body);
131+
} catch (parseError) {
132+
console.error('Failed to parse Vercel error body:', parseError);
133+
}
165134

135+
const errorData = errorBody?.error;
136+
137+
if (
138+
errorData?.code === 'domain_already_in_use' &&
139+
errorData?.projectId === env.TRUST_PORTAL_PROJECT_ID
140+
) {
141+
// Check if another organization already owns this domain in our database
142+
const existingDomainOwner = await db.trust.findFirst({
143+
where: {
144+
domain,
145+
organizationId: { not: activeOrganizationId },
146+
},
147+
select: { organizationId: true },
148+
});
149+
150+
if (existingDomainOwner) {
166151
return {
167-
success: true,
168-
needsVerification: true,
152+
success: false,
153+
error: 'Domain is already in use by another organization',
169154
};
170155
}
171-
} catch (parseError) {
172-
console.error('Failed to parse Vercel error body:', parseError);
156+
157+
// Domain already exists on our project - extract verification info and save it
158+
const domainInfo = errorData.domain;
159+
const vercelVerification = domainInfo?.verification?.[0]?.value || null;
160+
// Default to true since we're in the pending verification handler
161+
const isVercelDomain = domainInfo?.verified !== true;
162+
163+
console.log(
164+
`Domain ${domain} already exists on project, extracting verification info:`,
165+
vercelVerification,
166+
);
167+
168+
await db.trust.upsert({
169+
where: { organizationId: activeOrganizationId },
170+
update: {
171+
domain,
172+
domainVerified: false,
173+
isVercelDomain,
174+
vercelVerification,
175+
},
176+
create: {
177+
organizationId: activeOrganizationId,
178+
domain,
179+
domainVerified: false,
180+
isVercelDomain,
181+
vercelVerification,
182+
},
183+
});
184+
185+
revalidatePath(`/${activeOrganizationId}/trust`);
186+
revalidatePath(`/${activeOrganizationId}/trust/portal-settings`);
187+
revalidateTag(`organization_${activeOrganizationId}`, 'max');
188+
189+
return {
190+
success: true,
191+
needsVerification: true,
192+
};
173193
}
174194
}
175195

0 commit comments

Comments
 (0)