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

[Postgres] add constraint() to the DatabaseError trait, which covers most uses of downcasting #821

Closed
abonander opened this issue Nov 16, 2020 · 0 comments · Fixed by #998
Labels
db:postgres Related to PostgreSQL E-easy good first issue Good for newcomers

Comments

@abonander
Copy link
Collaborator

abonander commented Nov 16, 2020

In my experience, far and away the primary reason to use DatabaseError::downcast_ref() is to check the constraint name. It would be nice not to have to downcast, though:

https://github.com/launchbadge/sqlx/blob/master/sqlx-core/src/error.rs#L146

 pub trait DatabaseError: ... {
    // existing methods omitted

    /// Returns the name of the constraint that triggered the error, if applicable. 
    /// If the error was caused by a conflict of a unique index, this will be the index name.
    /// 
    /// ### Note
    /// Currently only populated by the Postgres driver.
    fn constraint(&self) -> Option<&str> { None }
}

This only needs to be overridden by PgDatabaseError as unfortunately none of the other databases have this context in their errors.

https://github.com/launchbadge/sqlx/blob/master/sqlx-core/src/postgres/error.rs#L164

impl DatabaseError for PgDatabaseError {
    // existing methods omitted

    fn constraint(&self) -> Option<&str> {
        // invokes the inherent method of the same name
        self.constraint()
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db:postgres Related to PostgreSQL E-easy good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant