Skip to content

Use custom message for UNIQUE constraint error message#2879

Open
rdwebdesign wants to merge 2 commits into
developmentfrom
tweak/unique_constraint_message
Open

Use custom message for UNIQUE constraint error message#2879
rdwebdesign wants to merge 2 commits into
developmentfrom
tweak/unique_constraint_message

Conversation

@rdwebdesign
Copy link
Copy Markdown
Member

@rdwebdesign rdwebdesign commented May 5, 2026

What does this PR aim to accomplish?

Currently, when an "UNIQUE constraint" error is found while adding items to group management tables, the returned message is not very user friendly.

This PR uses a custom error message.

image

Redo of #2878
Suggested in pi-hole/web#3773 (comment)

How does this PR accomplish the above?

Testing the returned error code.

If the error code is SQLITE_CONSTRAINT (19), we show a custom message.
We still show the original message for other error codes.


By submitting this pull request, I confirm the following:

  1. I have read and understood the contributors guide, as well as this entire template. I understand which branch to base my commits and Pull Requests against.
  2. I have commented my proposed changes within the code and I have tested my changes.
  3. I am willing to help maintain this change if there are issues with it later.
  4. It is compatible with the EUPL 1.2 license
  5. I have squashed any insignificant commits. (git rebase)
  6. I have checked that another pull request for this purpose does not exist.
  7. I have considered, and confirmed that this submission will be valuable to others.
  8. I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  9. I give this submission freely, and claim no ownership to its content.

  • I have read the above and my PR is ready for review. Check this box to confirm

@rdwebdesign rdwebdesign requested a review from a team as a code owner May 5, 2026 00:42
@rdwebdesign rdwebdesign force-pushed the tweak/unique_constraint_message branch from 28bd02e to 5a43ef0 Compare May 5, 2026 00:46
@rdwebdesign rdwebdesign requested a review from DL6ER May 5, 2026 02:32
Comment thread src/database/gravity-db.c Outdated
{
*message = sqlite3_errmsg(gravity_db);
if(rc == SQLITE_CONSTRAINT)
*message = "Item already in the database (UNIQUE constraint failed)";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*message = "Item already in the database (UNIQUE constraint failed)";
*message = "Record already present";

But maybe native speakers have an even better idea. Would need to be adapted in the other changes, too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.
The word "database" appears twice. Once on the title and once on the message. We can remove it here.

Also, the "(UNIQUE constraint failed)" can be removed. If was left here because there is code that (in some cases) expects this text, but I don't remember to ever see messages using that code.


I would like to suggest a different text, with "item" instead of "record".

The intention of this change is to return user friendly messages, specially for users without any programming or database background.

What do you think about "The item is already present"?

Suggested change
*message = "Item already in the database (UNIQUE constraint failed)";
*message = "The item is already present";

Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
…ssage

Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
@rdwebdesign rdwebdesign force-pushed the tweak/unique_constraint_message branch from c635777 to 144f044 Compare May 7, 2026 21:06
@rdwebdesign
Copy link
Copy Markdown
Member Author

rdwebdesign commented May 7, 2026

@DL6ER

I changed the messages. I force pushed to avoid unnecessary commits.

EDIT:

I noticed there are many other places showing "UNIQUE constraint failed" on the same 4 yaml files, like these:

The `database_error` with message `UNIQUE constraint failed` error indicates that this client already exists.

error: "UNIQUE constraint failed: client.ip"

error: "UNIQUE constraint failed: clientlist.client"


Should we change all of them?

@yubiuser yubiuser requested a review from DL6ER May 8, 2026 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants