Skip to content

The open alternative to Salesforce, designed for AI.

Report PDL enrichment failures and stop batches on rejected keys (#26168)

Raphaël Bosi26a02ecba1fa
diff --git a/packages/twenty-apps/public/people-data-labs/src/constants/pdl-access-error-message.ts b/packages/twenty-apps/public/people-data-labs/src/constants/pdl-access-error-message.ts
new file mode 100644
index 00000000000..abdf78005a6
--- /dev/null
+++ b/packages/twenty-apps/public/people-data-labs/src/constants/pdl-access-error-message.ts
@@ -0,0 +1,2 @@
+export const PDL_ACCESS_ERROR_MESSAGE =
+ 'People Data Labs enrichment is unavailable. Contact your workspace admin.';
diff --git a/packages/twenty-apps/public/people-data-labs/src/constants/pdl-account-error-http-statuses.ts b/packages/twenty-apps/public/people-data-labs/src/constants/pdl-account-error-http-statuses.ts
new file mode 100644
index 00000000000..6c085746686
--- /dev/null
+++ b/packages/twenty-apps/public/people-data-labs/src/constants/pdl-account-error-http-statuses.ts
@@ -0,0 +1,3 @@
+export const PDL_ACCOUNT_ERROR_HTTP_STATUSES: ReadonlySet<number> = new Set([
+ 401, 402,
+]);
diff --git a/packages/twenty-apps/public/people-data-labs/src/front-components/utils/__tests__/build-enrichment-snackbar-params.test.ts b/packages/twenty-apps/public/people-data-labs/src/front-components/utils/__tests__/build-enrichment-snackbar-params.test.ts
new file mode 100644
index 00000000000..abffdc751e9
--- /dev/null
+++ b/packages/twenty-apps/public/people-data-labs/src/front-components/utils/__tests__/build-enrichment-snackbar-params.test.ts
@@ -0,0 +1,69 @@
+import { describe, expect, it } from 'vitest';
+
+import { PDL_ACCESS_ERROR_MESSAGE } from 'src/constants/pdl-access-error-message';
+import { buildEnrichmentSnackbarParams } from 'src/front-components/utils/build-enrichment-snackbar-params';
+import { aggregateBulkEnrichResult } from 'src/logic-functions/utils/aggregate-bulk-enrich-result';
+import { buildErrorResult } from 'src/logic-functions/utils/build-error-result';
+import { buildMatchedResult } from 'src/logic-functions/utils/build-matched-result';
+import { buildNotFoundResult } from 'src/logic-functions/utils/build-not-found-result';
+
+describe('buildEnrichmentSnackbarParams', () => {
+ it('reports a success when no record failed', () => {
+ expect(
+ buildEnrichmentSnackbarParams(
+ aggregateBulkEnrichResult([
+ buildMatchedResult({ recordId: 'a', updatedFields: [] }),
+ buildNotFoundResult('b'),
+ ]),
+ ),
+ ).toEqual({ message: 'Enriched records.', variant: 'success' });
+ });
+
+ it('uses the singular form when a single record was enriched', () => {
+ expect(
+ buildEnrichmentSnackbarParams(
+ aggregateBulkEnrichResult([
+ buildMatchedResult({ recordId: 'a', updatedFields: [] }),
+ ]),
+ ),
+ ).toEqual({ message: 'Enriched record.', variant: 'success' });
+ });
+
+ it('reports an error with the first record error when every record failed', () => {
+ expect(
+ buildEnrichmentSnackbarParams(
+ aggregateBulkEnrichResult([
+ buildErrorResult({
+ recordId: 'a',
+ error: PDL_ACCESS_ERROR_MESSAGE,
+ }),
+ buildErrorResult({
+ recordId: 'b',
+ error: PDL_ACCESS_ERROR_MESSAGE,
+ }),
+ ]),
+ ),
+ ).toEqual({
+ message: 'Records enrichment failed',
+ variant: 'error',
+ detailedMessage:
+ 'People Data Labs enrichment is unavailable. Contact your workspace admin.',
+ });
+ });
+
+ it('reports a warning with the first record error when some records failed', () => {
+ expect(
+ buildEnrichmentSnackbarParams(
+ aggregateBulkEnrichResult([
+ buildMatchedResult({ recordId: 'a', updatedFields: [] }),
+ buildErrorResult({ recordId: 'b', error: 'update failed' }),
+ buildErrorResult({ recordId: 'c', error: 'build failed' }),
+ ]),
+ ),
+ ).toEqual({
+ message: 'Could not enrich 2 of 3 records.',
+ variant: 'warning',
+ detailedMessage: 'update failed',
+ });
+ });
+});
diff --git a/packages/twenty-apps/public/people-data-labs/src/front-components/utils/build-enrichment-snackbar-params.ts b/packages/twenty-apps/public/people-data-labs/src/front-components/utils/build-enrichment-snackbar-params.ts
new file mode 100644
index 00000000000..712f3dc51c8
--- /dev/null
+++ b/packages/twenty-apps/public/people-data-labs/src/front-components/utils/build-enrichment-snackbar-params.ts
@@ -0,0 +1,35 @@
+import { type EnqueueSnackbarParams } from 'twenty-sdk/front-component';
+
+import { type BulkEnrichResult } from 'src/types/bulk-enrich-result';
+import { isDefined } from 'src/utils/is-defined';
+
+export const buildEnrichmentSnackbarParams = ({
+ total,
+ errored,
+ results,
+}: BulkEnrichResult): EnqueueSnackbarParams => {
+ if (errored === 0) {
+ return {
+ message: `Enriched ${total > 1 ? 'records.' : 'record.'}`,
+ variant: 'success',
+ };
+ }
+
+ const firstErrorMessage = results.find((result) =>
+ isDefined(result.error),
+ )?.error;
+
+ if (errored === total) {
+ return {
+ message: 'Records enrichment failed',
+ variant: 'error',
+ detailedMessage: firstErrorMessage,
+ };
+ }
+
+ return {
+ message: `Could not enrich ${errored} of ${total} records.`,
+ variant: 'warning',
+ detailedMessage: firstErrorMessage,
+ };
+};
diff --git a/packages/twenty-apps/public/people-data-labs/src/front-components/utils/call-enrich-logic-function.utils.ts b/packages/twenty-apps/public/people-data-labs/src/front-components/utils/call-enrich-logic-function.utils.ts
index 3a2019b07ae..79aa9887ced 100644
--- a/packages/twenty-apps/public/people-data-labs/src/front-components/utils/call-enrich-logic-function.utils.ts
+++ b/packages/twenty-apps/public/people-data-labs/src/front-components/utils/call-enrich-logic-function.utils.ts
@@ -1,17 +1,24 @@
import { RestApiClient } from 'twenty-client-sdk/rest';
import { enqueueSnackbar } from 'twenty-sdk/front-component';
-export const execute = async ({path, recordIds}:{ path: string; recordIds: string[] }) => {
+import { buildEnrichmentSnackbarParams } from 'src/front-components/utils/build-enrichment-snackbar-params';
+import { type BulkEnrichResult } from 'src/types/bulk-enrich-result';
+
+export const execute = async ({
+ path,
+ recordIds,
+}: {
+ path: string;
+ recordIds: string[];
+}) => {
try {
const client = new RestApiClient();
-
- await client.post(`/s${path}`, { recordIds });
-
- await enqueueSnackbar({
- message: `Enriched ${recordIds.length > 1 ? 'records.' : 'record.'}`,
- variant: 'success',
+ const bulkEnrichResult = await client.post<BulkEnrichResult>(`/s${path}`, {
+ recordIds,
});
+
+ await enqueueSnackbar(buildEnrichmentSnackbarParams(bulkEnrichResult));
} catch {
await enqueueSnackbar({
message: 'Records enrichment failed',
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/is-pdl-account-error-outcome.test.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/is-pdl-account-error-outcome.test.ts
new file mode 100644
index 00000000000..f031543f300
--- /dev/null
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/is-pdl-account-error-outcome.test.ts
@@ -0,0 +1,39 @@
+import { describe, expect, it } from 'vitest';
+
+import { isPdlAccountErrorOutcome } from 'src/logic-functions/utils/is-pdl-account-error-outcome';
+
+describe('isPdlAccountErrorOutcome', () => {
+ it.each([401, 402])('flags an HTTP %i error', (httpStatus) => {
+ expect(
+ isPdlAccountErrorOutcome({
+ outcome: 'error',
+ httpStatus,
+ message: 'rejected',
+ }),
+ ).toBe(true);
+ });
+
+ it.each([429, 500])('does not flag an HTTP %i error', (httpStatus) => {
+ expect(
+ isPdlAccountErrorOutcome({
+ outcome: 'error',
+ httpStatus,
+ message: 'boom',
+ }),
+ ).toBe(false);
+ });
+
+ it('does not flag a matched outcome', () => {
+ expect(
+ isPdlAccountErrorOutcome({
+ outcome: 'matched',
+ httpStatus: 200,
+ data: { id: 'a' },
+ }),
+ ).toBe(false);
+ });
+
+ it('does not flag a missing outcome', () => {
+ expect(isPdlAccountErrorOutcome(undefined)).toBe(false);
+ });
+});
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/run-batch-enrichment.test.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/run-batch-enrichment.test.ts
index 457ee9c6000..239fd31a334 100644
--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/run-batch-enrichment.test.ts
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/run-batch-enrichment.test.ts
@@ -2,7 +2,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { type CoreApiClient } from 'twenty-client-sdk/core';
import { chargeCredits } from 'twenty-sdk/billing';
+import { PDL_ACCESS_ERROR_MESSAGE } from 'src/constants/pdl-access-error-message';
import { UPDATE_FIELDS_OPTIONS } from 'src/constants/update-fields-options';
+import { PdlConfigError } from 'src/logic-functions/errors/pdl-config-error';
import { runBatchEnrichment } from 'src/logic-functions/utils/run-batch-enrichment';
import { type BatchEnrichmentAdapter } from 'src/types/batch-enrichment-adapter';
import { type PdlEnrichResult } from 'src/types/pdl-enrich-result';
@@ -130,6 +132,15 @@ const isPresent = <TValue>(value: TValue | undefined): value is TValue =>
const records = (...ids: string[]) => ids.map((id) => ({ id }));
+const buildRecordIds = (count: number) =>
+ Array.from({ length: count }, (_unused, index) => `r${index}`);
+
+const INVALID_API_KEY_OUTCOME: PdlEnrichResult<FakeData> = {
+ outcome: 'error',
+ httpStatus: 401,
+ message: 'Invalid API key',
+};
+
describe('runBatchEnrichment', () => {
beforeEach(() => {
vi.mocked(chargeCredits).mockClear();
@@ -405,7 +416,7 @@ describe('runBatchEnrichment', () => {
});
it('chunks large id sets into separate read and PDL calls', async () => {
- const ids = Array.from({ length: 150 }, (_unused, index) => `r${index}`);
+ const ids = buildRecordIds(150);
const harness = buildHarness(ids.map((id) => ({ id })));
const result = await runBatchEnrichment({
@@ -419,6 +430,75 @@ describe('runBatchEnrichment', () => {
expect(result.matched).toBe(150);
});
+ it('stops enriching the remaining chunks when PDL rejects the API key', async () => {
+ const ids = buildRecordIds(150);
+ const harness = buildHarness(
+ ids.map((id) => ({ id, outcome: INVALID_API_KEY_OUTCOME })),
+ );
+
+ const result = await runBatchEnrichment({
+ client: CLIENT,
+ input: { records: ids.map((id) => ({ id })) },
+ adapter: harness.adapter,
+ });
+
+ expect(harness.readRecords).toHaveBeenCalledTimes(1);
+ expect(harness.enrichBatch).toHaveBeenCalledTimes(1);
+ expect(harness.updateManyStatus).toHaveBeenCalledExactlyOnceWith({
+ client: CLIENT,
+ recordIds: ids.slice(0, 100),
+ data: {
+ pdlEnrichmentStatus: 'ERROR',
+ pdlLastEnrichedAt: expect.any(String),
+ },
+ });
+ expect(result).toMatchObject({ total: 150, errored: 150, success: false });
+ expect(result.results[0]).toMatchObject({
+ recordId: 'r0',
+ status: 'ERROR',
+ error: PDL_ACCESS_ERROR_MESSAGE,
+ });
+ expect(result.results[149]).toMatchObject({
+ recordId: 'r149',
+ status: 'ERROR',
+ error: PDL_ACCESS_ERROR_MESSAGE,
+ });
+ });
+
+ it('stops enriching the remaining chunks when the API key is not configured', async () => {
+ const ids = buildRecordIds(150);
+ const harness = buildHarness(ids.map((id) => ({ id })));
+ harness.enrichBatch.mockRejectedValue(
+ new PdlConfigError('PDL_API_KEY is not set.'),
+ );
+
+ const result = await runBatchEnrichment({
+ client: CLIENT,
+ input: { records: ids.map((id) => ({ id })) },
+ adapter: harness.adapter,
+ });
+
+ expect(harness.enrichBatch).toHaveBeenCalledTimes(1);
+ expect(result).toMatchObject({ total: 150, errored: 150 });
+ expect(result.results[0].error).toBe(PDL_ACCESS_ERROR_MESSAGE);
+ expect(result.results[149].error).toBe(PDL_ACCESS_ERROR_MESSAGE);
+ });
+
+ it('keeps enriching the remaining chunks after a chunk fails for another reason', async () => {
+ const ids = buildRecordIds(150);
+ const harness = buildHarness(ids.map((id) => ({ id })));
+ harness.enrichBatch.mockRejectedValueOnce(new Error('pdl down'));
+
+ const result = await runBatchEnrichment({
+ client: CLIENT,
+ input: { records: ids.map((id) => ({ id })) },
+ adapter: harness.adapter,
+ });
+
+ expect(harness.enrichBatch).toHaveBeenCalledTimes(2);
+ expect(result).toMatchObject({ matched: 50, errored: 100 });
+ });
+
it('bills only matched outcomes after the PDL call', async () => {
const harness = buildHarness([
{ id: 'a' },
@@ -475,7 +555,7 @@ describe('runBatchEnrichment', () => {
});
it('bills each chunk separately', async () => {
- const ids = Array.from({ length: 150 }, (_unused, index) => `r${index}`);
+ const ids = buildRecordIds(150);
const harness = buildHarness(ids.map((id) => ({ id })));
await runBatchEnrichment({
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/to-pdl-outcome-error-message.test.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/to-pdl-outcome-error-message.test.ts
new file mode 100644
index 00000000000..b3804bd4149
--- /dev/null
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/__tests__/to-pdl-outcome-error-message.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from 'vitest';
+
+import { toPdlOutcomeErrorMessage } from 'src/logic-functions/utils/to-pdl-outcome-error-message';
+
+describe('toPdlOutcomeErrorMessage', () => {
+ it.each([401, 402])(
+ 'hides the People Data Labs message behind the access message on an HTTP %i error',
+ (httpStatus) => {
+ expect(
+ toPdlOutcomeErrorMessage({
+ outcome: 'error',
+ httpStatus,
+ message: 'Invalid API key',
+ }),
+ ).toBe(
+ 'People Data Labs enrichment is unavailable. Contact your workspace admin.',
+ );
+ },
+ );
+
+ it('keeps the People Data Labs message for any other error', () => {
+ expect(
+ toPdlOutcomeErrorMessage({
+ outcome: 'error',
+ httpStatus: 429,
+ message: 'Rate limited',
+ }),
+ ).toBe('Rate limited');
+ });
+
+ it('reports a missing outcome', () => {
+ expect(toPdlOutcomeErrorMessage(undefined)).toBe(
+ 'People Data Labs returned no response for this record.',
+ );
+ });
+});
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/enrich-chunk.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/enrich-chunk.ts
index 328e1289a32..2cdd1d4a261 100644
--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/enrich-chunk.ts
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/enrich-chunk.ts
@@ -1,16 +1,21 @@
import { type CoreApiClient } from 'twenty-client-sdk/core';
+import { PDL_ACCESS_ERROR_MESSAGE } from 'src/constants/pdl-access-error-message';
+import { PdlConfigError } from 'src/logic-functions/errors/pdl-config-error';
import { buildErrorResult } from 'src/logic-functions/utils/build-error-result';
import { buildMatchedResult } from 'src/logic-functions/utils/build-matched-result';
import { buildNotFoundResult } from 'src/logic-functions/utils/build-not-found-result';
import { buildSkippedResult } from 'src/logic-functions/utils/build-skipped-result';
import { chargeMatchedEnrichments } from 'src/logic-functions/utils/charge-matched-enrichments';
import { INTERNAL_BOOKKEEPING_FIELDS } from 'src/logic-functions/utils/internal-field-names';
+import { isPdlAccountErrorOutcome } from 'src/logic-functions/utils/is-pdl-account-error-outcome';
import { nowIso } from 'src/logic-functions/utils/now-iso';
import { resolveUpdateFieldsMode } from 'src/logic-functions/utils/resolve-update-fields-mode';
+import { toPdlOutcomeErrorMessage } from 'src/logic-functions/utils/to-pdl-outcome-error-message';
import { type BatchEnrichmentAdapter } from 'src/types/batch-enrichment-adapter';
import { type BulkEnrichInput } from 'src/types/bulk-enrich-input';
import { type CompanyIdByMatchKeyCache } from 'src/types/company-id-by-match-key-cache';
+import { type EnrichChunkResult } from 'src/types/enrich-chunk-result';
import { type EnrichResult } from 'src/types/enrich-result';
import { type PdlEnrichResult } from 'src/types/pdl-enrich-result';
import { isDefined } from 'src/utils/is-defined';
@@ -185,7 +190,7 @@ export const enrichChunk = async <TNode, TData, TParams>({
adapter: BatchEnrichmentAdapter<TNode, TData, TParams>;
resultById: Map<string, EnrichResult>;
companyIdByMatchKeyCache: CompanyIdByMatchKeyCache;
-}): Promise<void> => {
+}): Promise<EnrichChunkResult> => {
const { shouldPersist, overrideExistingValues } = resolveUpdateFieldsMode(
input.updateFields,
);
@@ -202,7 +207,7 @@ export const enrichChunk = async <TNode, TData, TParams>({
);
}
- return;
+ return {};
}
const nodeByRecordId = new Map(
@@ -240,7 +245,7 @@ export const enrichChunk = async <TNode, TData, TParams>({
}
if (recordsToEnrich.length === 0) {
- return;
+ return {};
}
const enrichedAt = nowIso();
@@ -252,7 +257,10 @@ export const enrichChunk = async <TNode, TData, TParams>({
recordsToEnrich.map((recordToEnrich) => recordToEnrich.params),
);
} catch (enrichBatchError) {
- const enrichBatchErrorMessage = toErrorMessage(enrichBatchError);
+ const isPdlConfigError = enrichBatchError instanceof PdlConfigError;
+ const enrichBatchErrorMessage = isPdlConfigError
+ ? PDL_ACCESS_ERROR_MESSAGE
+ : toErrorMessage(enrichBatchError);
for (const recordToEnrich of recordsToEnrich) {
resultById.set(
recordToEnrich.recordId,
@@ -273,7 +281,9 @@ export const enrichChunk = async <TNode, TData, TParams>({
});
}
- return;
+ return isPdlConfigError
+ ? { pdlAccessErrorMessage: enrichBatchErrorMessage }
+ : {};
}
await chargeMatchedEnrichments({
@@ -292,12 +302,12 @@ export const enrichChunk = async <TNode, TData, TParams>({
const enrichmentOutcome = pdlEnrichmentOutcomes[index];
if (!isDefined(enrichmentOutcome) || enrichmentOutcome.outcome === 'error') {
- const enrichmentErrorMessage = isDefined(enrichmentOutcome)
- ? enrichmentOutcome.message
- : 'People Data Labs returned no response for this record.';
resultById.set(
recordId,
- buildErrorResult({ recordId, error: enrichmentErrorMessage }),
+ buildErrorResult({
+ recordId,
+ error: toPdlOutcomeErrorMessage(enrichmentOutcome),
+ }),
);
recordIdsToMarkAsError.push(recordId);
continue;
@@ -358,4 +368,14 @@ export const enrichChunk = async <TNode, TData, TParams>({
enrichedAt,
});
}
+
+ const hasOnlyPdlAccountErrorOutcomes =
+ pdlEnrichmentOutcomes.length > 0 &&
+ pdlEnrichmentOutcomes.every(isPdlAccountErrorOutcome);
+
+ return {
+ pdlAccessErrorMessage: hasOnlyPdlAccountErrorOutcomes
+ ? PDL_ACCESS_ERROR_MESSAGE
+ : undefined,
+ };
};
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/is-pdl-account-error-outcome.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/is-pdl-account-error-outcome.ts
new file mode 100644
index 00000000000..a6db075ab67
--- /dev/null
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/is-pdl-account-error-outcome.ts
@@ -0,0 +1,8 @@
+import { PDL_ACCOUNT_ERROR_HTTP_STATUSES } from 'src/constants/pdl-account-error-http-statuses';
+import { type PdlEnrichResult } from 'src/types/pdl-enrich-result';
+
+export const isPdlAccountErrorOutcome = <TData>(
+ enrichmentOutcome: PdlEnrichResult<TData> | undefined,
+): boolean =>
+ enrichmentOutcome?.outcome === 'error' &&
+ PDL_ACCOUNT_ERROR_HTTP_STATUSES.has(enrichmentOutcome.httpStatus);
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/run-batch-enrichment.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/run-batch-enrichment.ts
index 7162a70d87f..7a1e0850334 100644
--- a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/run-batch-enrichment.ts
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/run-batch-enrichment.ts
@@ -13,6 +13,7 @@ import { type BulkEnrichInput } from 'src/types/bulk-enrich-input';
import { type BulkEnrichResult } from 'src/types/bulk-enrich-result';
import { type CompanyIdByMatchKeyCache } from 'src/types/company-id-by-match-key-cache';
import { type EnrichResult } from 'src/types/enrich-result';
+import { isDefined } from 'src/utils/is-defined';
const PDL_BATCH_SIZE = 100;
@@ -28,9 +29,10 @@ export const runBatchEnrichment = async <TNode, TData, TParams>({
const recordIds = Array.from(new Set(extractRecordIds(input.records)));
const resultById = new Map<string, EnrichResult>();
const companyIdByMatchKeyCache: CompanyIdByMatchKeyCache = new Map();
+ let pdlAccessErrorMessage: string | undefined;
for (const recordIdsChunk of chunk({ items: recordIds, size: PDL_BATCH_SIZE })) {
- await enrichChunk({
+ const enrichChunkResult = await enrichChunk({
client,
recordIds: recordIdsChunk,
input,
@@ -38,12 +40,21 @@ export const runBatchEnrichment = async <TNode, TData, TParams>({
resultById,
companyIdByMatchKeyCache,
});
+
+ pdlAccessErrorMessage = enrichChunkResult.pdlAccessErrorMessage;
+
+ if (isDefined(pdlAccessErrorMessage)) {
+ break;
+ }
}
const results = recordIds.map(
(recordId) =>
resultById.get(recordId) ??
- buildErrorResult({ recordId, error: ENRICHMENT_FAILED_MESSAGE }),
+ buildErrorResult({
+ recordId,
+ error: pdlAccessErrorMessage ?? ENRICHMENT_FAILED_MESSAGE,
+ }),
);
return aggregateBulkEnrichResult(results);
diff --git a/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/to-pdl-outcome-error-message.ts b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/to-pdl-outcome-error-message.ts
new file mode 100644
index 00000000000..72e2de52abf
--- /dev/null
+++ b/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/to-pdl-outcome-error-message.ts
@@ -0,0 +1,21 @@
+import { PDL_ACCESS_ERROR_MESSAGE } from 'src/constants/pdl-access-error-message';
+import { isPdlAccountErrorOutcome } from 'src/logic-functions/utils/is-pdl-account-error-outcome';
+import { type PdlEnrichResult } from 'src/types/pdl-enrich-result';
+import { isDefined } from 'src/utils/is-defined';
+
+const NO_RESPONSE_MESSAGE =
+ 'People Data Labs returned no response for this record.';
+
+type PdlErrorOutcome = Extract<PdlEnrichResult<unknown>, { outcome: 'error' }>;
+
+export const toPdlOutcomeErrorMessage = (
+ enrichmentOutcome: PdlErrorOutcome | undefined,
+): string => {
+ if (!isDefined(enrichmentOutcome)) {
+ return NO_RESPONSE_MESSAGE;
+ }
+
+ return isPdlAccountErrorOutcome(enrichmentOutcome)
+ ? PDL_ACCESS_ERROR_MESSAGE
+ : enrichmentOutcome.message;
+};
diff --git a/packages/twenty-apps/public/people-data-labs/src/types/enrich-chunk-result.ts b/packages/twenty-apps/public/people-data-labs/src/types/enrich-chunk-result.ts
new file mode 100644
index 00000000000..df5b1019bd1
--- /dev/null
+++ b/packages/twenty-apps/public/people-data-labs/src/types/enrich-chunk-result.ts
@@ -0,0 +1,3 @@
+export type EnrichChunkResult = {
+ pdlAccessErrorMessage?: string;
+};