The minimalistic document archiving platform.
fix(errors): return 409 status code for existing tag errors in API (#992)
Corentin Thomasset9039b4806eba
diff --git a/.changeset/six-readers-flow.md b/.changeset/six-readers-flow.mdnew file mode 100644index 00000000..f3e64ccf--- /dev/null+++ b/.changeset/six-readers-flow.md@@ -0,0 +1,5 @@+---+"@papra/app": patch+---++Properly return a "document already has tag" error when trying to add a tag to a document that already has it, instead of a generic 500 error when using an hosted Turso db.diff --git a/.changeset/tasty-parks-buy.md b/.changeset/tasty-parks-buy.mdnew file mode 100644index 00000000..1d7c32f1--- /dev/null+++ b/.changeset/tasty-parks-buy.md@@ -0,0 +1,5 @@+---+"@papra/app": patch+---++Api now returns a 409 status code instead of a 400 when either creating a tag that already exists or adding a tag to a document that already has it.diff --git a/apps/papra-server/src/modules/tags/tags.errors.ts b/apps/papra-server/src/modules/tags/tags.errors.tsindex ed730a48..5292232c 100644--- a/apps/papra-server/src/modules/tags/tags.errors.ts+++ b/apps/papra-server/src/modules/tags/tags.errors.ts@@ -3,13 +3,13 @@ import { createErrorFactory } from '../shared/errors/errors';export const createDocumentAlreadyHasTagError = createErrorFactory({message: 'Document already has tag',code: 'documents.already_has_tag',- statusCode: 400,+ statusCode: 409,});export const createTagAlreadyExistsError = createErrorFactory({message: 'Tag already exists',code: 'tags.already_exists',- statusCode: 400,+ statusCode: 409,});export const createTagNotFoundError = createErrorFactory({diff --git a/apps/papra-server/src/modules/tags/tags.repository.ts b/apps/papra-server/src/modules/tags/tags.repository.tsindex 20ae8772..95c4931f 100644--- a/apps/papra-server/src/modules/tags/tags.repository.ts+++ b/apps/papra-server/src/modules/tags/tags.repository.ts@@ -2,7 +2,6 @@ import type { Database } from '../app/database/database.types';import type { Tag } from './tags.types';import { injectArguments, safely } from '@corentinth/chisels';import { and, count, desc, eq, getTableColumns, inArray, sql } from 'drizzle-orm';-import { get } from 'lodash-es';import { documentsTable } from '../documents/documents.table';import { isUniqueConstraintError } from '../shared/db/constraints.models';import { isDefined, omitUndefined } from '../shared/utils';@@ -155,7 +154,7 @@ async function updateTag({ tagId, name, description, color, db }: { tagId: strinasync function addTagToDocument({ tagId, documentId, db }: { tagId: string; documentId: string; db: Database }) {const [_, error] = await safely(db.insert(documentsTagsTable).values({ tagId, documentId }));- if (error && get(error, 'code') === 'SQLITE_CONSTRAINT_PRIMARYKEY') {+ if (error && isUniqueConstraintError({ error })) {throw createDocumentAlreadyHasTagError();}