Files
klz-cables.com/.pnpm-store/v10/files/32/971e56f0d318b1cb6a844aa9a711224d117e165e39f0ea78e0b508ffc21ea37bb122ae549b73776c235802413dc66b6dd1d1640cbca3efe2dc96ee5fd0672e
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

65 lines
2.8 KiB
Plaintext

import { ValidationError } from 'payload';
/**
* Handles unique constraint violation errors from PostgreSQL and SQLite,
* converting them to Payload ValidationErrors.
* Re-throws non-constraint errors unchanged.
*/ export const handleUpsertError = ({ id, adapter, collectionSlug, error: caughtError, globalSlug, req, tableName })=>{
let error = caughtError;
if (typeof caughtError === 'object' && caughtError !== null && 'cause' in caughtError) {
error = caughtError.cause;
}
// PostgreSQL: 23505, SQLite: SQLITE_CONSTRAINT_UNIQUE
if (error?.code === '23505' || error?.code === 'SQLITE_CONSTRAINT_UNIQUE') {
let fieldName = null;
if (error.code === '23505') {
// PostgreSQL - extract field name from constraint
if (adapter.fieldConstraints?.[tableName]?.[error.constraint]) {
fieldName = adapter.fieldConstraints[tableName][error.constraint];
} else {
const replacement = `${tableName}_`;
if (error.constraint?.includes(replacement)) {
const replacedConstraint = error.constraint.replace(replacement, '');
if (replacedConstraint && adapter.fieldConstraints[tableName]?.[replacedConstraint]) {
fieldName = adapter.fieldConstraints[tableName][replacedConstraint];
}
}
}
if (!fieldName && error.detail) {
// Extract from detail: "Key (field)=(value) already exists."
const regex = /Key \(([^)]+)\)=\(([^)]+)\)/;
const match = error.detail.match(regex);
if (match && match[1]) {
fieldName = match[1];
}
}
} else if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
// SQLite - extract from message: "UNIQUE constraint failed: table.field"
const regex = /UNIQUE constraint failed: ([^.]+)\.([^.]+)/;
const match = error.message?.match(regex);
if (match && match[2]) {
if (adapter.fieldConstraints[tableName]) {
fieldName = adapter.fieldConstraints[tableName][`${match[2]}_idx`];
}
if (!fieldName) {
fieldName = match[2];
}
}
}
throw new ValidationError({
id,
collection: collectionSlug,
errors: [
{
message: req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique',
path: fieldName
}
],
global: globalSlug,
req
}, req?.t);
}
// Re-throw non-constraint errors
throw caughtError;
};
//# sourceMappingURL=handleUpsertError.js.map