Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database error text not being shown in admin UI #167

Closed
ethagnawl opened this issue Jun 13, 2022 · 8 comments
Closed

Database error text not being shown in admin UI #167

ethagnawl opened this issue Jun 13, 2022 · 8 comments
Labels
enhancement New feature or request

Comments

@ethagnawl
Copy link
Contributor

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.

@dantownsend
Copy link
Member

dantownsend commented 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 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.

@sinisaos
Copy link
Member

sinisaos commented Jun 14, 2022

We can use generic exception to catch database errors on post_single, put_single and patch_single in Piccolo API crud endpoints like this:

except Exception as e:
    return Response(
        f"Unable to save the resource: {e}", status_code=400
    )

Result is
error

or in Postgres with biggest alert box

error

@dantownsend
Copy link
Member

@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
Copy link
Member

@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

from piccolo.engine.exceptions import UniqueConstraintError # or different name of exception
...
except UniqueConstraintError as exception:
    return Response(
        f"Unable to save the resource: {exception}", status_code=400
    )
...

@dantownsend
Copy link
Member

dantownsend commented 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
Copy link
Contributor Author

Thanks for the prompt response, @sinisaos and @dantownsend! Any of the above would make for a much better UX.

@dantownsend
Copy link
Member

dantownsend commented 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 dantownsend added the enhancement New feature or request label Sep 2, 2022
@dantownsend
Copy link
Member

dantownsend commented Sep 2, 2022

In piccolo_api>=0.47.0 this should be fixed.

I'll create some follow up tasks, as there's still more we can do with error messages in Piccolo Admin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants