diff --git a/mealie/core/exceptions.py b/mealie/core/exceptions.py index 781a71a66..11cc8f968 100644 --- a/mealie/core/exceptions.py +++ b/mealie/core/exceptions.py @@ -1,4 +1,4 @@ -from sqlite3 import IntegrityError +from sqlalchemy.exc import IntegrityError from mealie.lang.providers import Translator diff --git a/mealie/routes/_base/mixins.py b/mealie/routes/_base/mixins.py index e69457c78..6ccb38e47 100644 --- a/mealie/routes/_base/mixins.py +++ b/mealie/routes/_base/mixins.py @@ -58,6 +58,11 @@ class HttpRepo[C: BaseModel, R: BaseModel, U: BaseModel]: status.HTTP_404_NOT_FOUND, detail=ErrorResponse.respond(message=msg, exception=str(ex)), ) + elif isinstance(ex, sqlalchemy.exc.IntegrityError): + raise HTTPException( + status.HTTP_409_CONFLICT, + detail=ErrorResponse.respond(message=msg, exception=str(ex)), + ) else: raise HTTPException( status.HTTP_400_BAD_REQUEST, diff --git a/tests/integration_tests/category_tag_tool_tests/test_organizers_common.py b/tests/integration_tests/category_tag_tool_tests/test_organizers_common.py index 462aba3d8..424276147 100644 --- a/tests/integration_tests/category_tag_tool_tests/test_organizers_common.py +++ b/tests/integration_tests/category_tag_tool_tests/test_organizers_common.py @@ -192,7 +192,7 @@ def test_organizer_association( @pytest.mark.parametrize("route", organizer_routes, ids=test_ids) -def test_organizer_create_duplicate_name_returns_400( +def test_organizer_create_duplicate_name_returns_409( api_client: TestClient, unique_user: TestUser, route: RoutesBase, @@ -206,7 +206,7 @@ def test_organizer_create_duplicate_name_returns_400( assert response.status_code == 201 response = api_client.post(route.base, json=data, headers=unique_user.token) - assert response.status_code == 400 + assert response.status_code == 409 @pytest.mark.parametrize("route, recipe_key", association_data, ids=test_ids) diff --git a/tests/integration_tests/user_household_tests/test_shopping_list_labels.py b/tests/integration_tests/user_household_tests/test_shopping_list_labels.py index 791df417c..00acc2eb4 100644 --- a/tests/integration_tests/user_household_tests/test_shopping_list_labels.py +++ b/tests/integration_tests/user_household_tests/test_shopping_list_labels.py @@ -20,7 +20,7 @@ def create_labels(api_client: TestClient, unique_user: TestUser, count: int = 10 return labels -def test_label_create_duplicate_name_returns_400(api_client: TestClient, unique_user_fn_scoped: TestUser): +def test_label_create_duplicate_name_returns_409(api_client: TestClient, unique_user_fn_scoped: TestUser): # Regression test for #7582: POSTing a duplicate label name leaked the # sqlalchemy IntegrityError as an HTTP 500. The expected behavior, matching # the other organizer endpoints (foods, units, tools, tags, categories), @@ -32,7 +32,7 @@ def test_label_create_duplicate_name_returns_400(api_client: TestClient, unique_ assert response.status_code == 200 response = api_client.post(api_routes.groups_labels, json=payload, headers=unique_user_fn_scoped.token) - assert response.status_code == 400 + assert response.status_code == 409 def test_new_list_creates_list_labels(api_client: TestClient, unique_user: TestUser):