The minimalistic document archiving platform.
fix(errors): enhance isUniqueConstraintError to handle hosted libsql dbs (#623)
Corentin Thomassetb0877645a868
diff --git a/.changeset/violet-buckets-fold.md b/.changeset/violet-buckets-fold.mdnew file mode 100644index 00000000..602134c9--- /dev/null+++ b/.changeset/violet-buckets-fold.md@@ -0,0 +1,5 @@+---+"@papra/docker": patch+---++Improved unique constraints error when dealing with hosted libsql dbdiff --git a/apps/papra-server/src/modules/shared/db/constraints.models.test.ts b/apps/papra-server/src/modules/shared/db/constraints.models.test.tsindex 2af47c5b..4bee90e2 100644--- a/apps/papra-server/src/modules/shared/db/constraints.models.test.ts+++ b/apps/papra-server/src/modules/shared/db/constraints.models.test.ts@@ -12,5 +12,9 @@ describe('constraints models', () => {expect(isUniqueConstraintError({ error: null })).to.eql(false);expect(isUniqueConstraintError({ error: new Set([Symbol('bob')]) })).to.eql(false);});++ test('when dealing with turso cloud db the error does not have the standard code but the message contains "unique constraint failed"', () => {+ expect(isUniqueConstraintError({ error: { message: 'SQLITE_CONSTRAINT: SQLite error: UNIQUE constraint failed: tags.organization_id, tags.name' } })).to.eql(true);+ });});});diff --git a/apps/papra-server/src/modules/shared/db/constraints.models.ts b/apps/papra-server/src/modules/shared/db/constraints.models.tsindex ac4ce17f..e9a75530 100644--- a/apps/papra-server/src/modules/shared/db/constraints.models.ts+++ b/apps/papra-server/src/modules/shared/db/constraints.models.ts@@ -1,7 +1,12 @@import { get } from 'lodash-es';-export { isUniqueConstraintError };+export function isUniqueConstraintError({ error }: { error: unknown }): boolean {+ const message: unknown = get(error, 'message');+ const code: unknown = get(error, 'code');-function isUniqueConstraintError({ error }: { error: unknown }): boolean {- return get(error, 'code') === 'SQLITE_CONSTRAINT_UNIQUE';+ if (code === 'SQLITE_CONSTRAINT_UNIQUE') {+ return true;+ }++ return typeof message === 'string' && message.toLowerCase().includes('unique constraint failed');}