You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Database error text not being shown in admin UI #167
I'm seeing some (potentially) unexpected behavior when encountering database-level input validation errors.
For example, if I have a table (app_user) whose name column has a uniqueness constraint on it, an admin entering a duplicate name is met with a 500 HTTP response whose "Internal Server Error" body is displayed to the user upon submitting the form. This isn't very helpful to a non-technical user.
Is this the expected behavior and the burden is on the implementer to handle the form validation and error messaging or should the error message returned to the UI contain the contents of the database's error message? From looking through endpoints.py, I think it should be the latter (400 response with {"message": ...} body) but I'm not certain.
Unique constraints aren't handled well currently - it should return a proper error message, as you say.
Most of the Piccolo Admin backend is built upon a class called PiccoloCRUD which maps HTTP methods to SQL queries. We rely heavily on Pydantic for input validation. But Pydantic can't validate unique constraints - we need to rely on the database. This is the relevant method on PiccoloCRUD.
We need to catch the database errors, and returns something more useful.
Ideally we would be able to just catch certain exceptions, for example unique errors. There's a PR open in Piccolo, but I haven't finished it yet. The idea is it will map the asyncpg or aiosqlite exceptions into generic Piccolo exceptions (for example UniqueException). For now though, this might be our best option.
@dantownsend Like you said that would be ideal because they would have the same error message for different db drivers. Once you finish with Piccolo PR you can mention me in the comments and I can do PR in Piccolo API and Piccolo Admin to implement this feature. Something like this
frompiccolo.engine.exceptionsimportUniqueConstraintError# or different name of exception
...
exceptUniqueConstraintErrorasexception:
returnResponse(
f"Unable to save the resource: {exception}", status_code=400
)
...
Sorry, progress on this stalled. Going with something similar to @sinisaos solution (piccolo-orm/piccolo_api#187). It will now show a proper error message if a unique constraint fails rather than just a 500.
Activity
dantownsend commentedon Jun 13, 2022
Unique constraints aren't handled well currently - it should return a proper error message, as you say.
Most of the Piccolo Admin backend is built upon a class called
PiccoloCRUDwhich maps HTTP methods to SQL queries. We rely heavily on Pydantic for input validation. But Pydantic can't validate unique constraints - we need to rely on the database. This is the relevant method on PiccoloCRUD.We need to catch the database errors, and returns something more useful.
sinisaos commentedon Jun 14, 2022
We can use generic exception to catch database errors on
post_single,put_singleandpatch_singlein Piccolo API crud endpoints like this:Result is

or in Postgres with biggest alert box
dantownsend commentedon Jun 14, 2022
@sinisaos Thanks, that could work.
Ideally we would be able to just catch certain exceptions, for example unique errors. There's a PR open in Piccolo, but I haven't finished it yet. The idea is it will map the asyncpg or aiosqlite exceptions into generic Piccolo exceptions (for example UniqueException). For now though, this might be our best option.
sinisaos commentedon Jun 14, 2022
@dantownsend Like you said that would be ideal because they would have the same error message for different db drivers. Once you finish with Piccolo PR you can mention me in the comments and I can do PR in Piccolo API and Piccolo Admin to implement this feature. Something like this
dantownsend commentedon Jun 14, 2022
@sinisaos Cool, thanks - that looks good.
If I don't manage to get that PR closed soon, we'll go with your original solution.
ethagnawl commentedon Jun 14, 2022
Thanks for the prompt response, @sinisaos and @dantownsend! Any of the above would make for a much better UX.
dantownsend commentedon Sep 1, 2022
Sorry, progress on this stalled. Going with something similar to @sinisaos solution (piccolo-orm/piccolo_api#187). It will now show a proper error message if a unique constraint fails rather than just a 500.
dantownsend commentedon Sep 2, 2022
In
piccolo_api>=0.47.0this should be fixed.I'll create some follow up tasks, as there's still more we can do with error messages in Piccolo Admin.