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

Better error system #1002

Merged
merged 15 commits into from
Oct 6, 2022
Merged

Better error system #1002

merged 15 commits into from
Oct 6, 2022

Conversation

tyt2y3
Copy link
Member

@tyt2y3 tyt2y3 commented Aug 28, 2022

Continue #750

mohs8421 and others added 2 commits August 28, 2022 11:13
* feat: Introducing feature "sqlx-error"

Purpose of this feature is to not convert errors given from sqlx into strings to ease further analysis of the error and react to it accordingly. This implementation uses a feature switch and an additional error kind to avoid interfering with existing implementations without this feature enabled.
See discussion #709

* fix: Align feature "sqlx-error" with merged Migration error kind

Due to the merge, an other error kind had been introduced and the DbErr became Eq and Clone, however Eq cannot easily be derived from, so I went back to PartialEq, and since the sqlx error does not implement clone, this was converted into an Arc, to allow cloning of the new kind too.

* fix: Repairing failing jobs

Several jobs had failed as I missed to correct the return values of a few methods in transaction.rs and had a wrong understanding of map_err at that point.

* feat: realigning with latest changes in sea-orm, different approach

Instead of the former approach to introduce a new error kind, now the existing error types get extended, for now only Exec and Query, because these are the most relevant for the requirement context.
Afterwards it might still be possible to add some further detail information.
See discussion #709

* Update src/driver/sqlx_mysql.rs

Integrating fixes done by @Sculas

Co-authored-by: Sculas <[email protected]>

* Update src/driver/sqlx_postgres.rs

Integrating fixes done by @Sculas

Co-authored-by: Sculas <[email protected]>

* Update src/driver/sqlx_sqlite.rs

Integrating fixes done by @Sculas

Co-authored-by: Sculas <[email protected]>

* feat: reworking feature with thiserror

Following the latest suggestions I changed the implementation to utilize `thiserror`.
Now there are more error kinds to be able to see the different kinds directly in src/error.rs
To ensure the behaviour is as expected, I also introduce a further test, which checks for a uniqueness failure.

Co-authored-by: Sculas <[email protected]>
@tyt2y3
Copy link
Member Author

tyt2y3 commented Aug 28, 2022

@mohs8421

Comment on lines 94 to 103
let entity_name = data_enum
.variants
.first()
.map(|column| {
let column_iden = column.ident.clone();
quote!(
#ident::#column_iden.entity_name().to_string()
)
})
.unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

Hey @tyt2y3, yeah, this isn't possible considering we can DeriveColumn on Enum that is not necessarily a Entity::Column.

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

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

Good to go!
Thanks!! @mohs8421 @tyt2y3

Copy link
Contributor

@Sculas Sculas left a comment

Choose a reason for hiding this comment

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

I fixed some typos and consistency in the error messages.

src/error.rs Outdated Show resolved Hide resolved
src/error.rs Outdated Show resolved Hide resolved
src/error.rs Outdated Show resolved Hide resolved
src/error.rs Outdated Show resolved Hide resolved
tyt2y3 and others added 4 commits September 3, 2022 20:44
@Sculas
Copy link
Contributor

Sculas commented Sep 7, 2022

Any updates on this?

@billy1624
Copy link
Member

Hey @Sculas, we'll do a final review then merge it :)

@tyt2y3
Copy link
Member Author

tyt2y3 commented Sep 7, 2022

Actually I strongly appreciate another pair of eyes to look at this

@Sculas
Copy link
Contributor

Sculas commented Sep 7, 2022

Would an error like UniqueConstraintError be out of scope for SeaORM? If not, maybe some other uncommon errors too? Just to make it easier for the user. If it's not out of scope I'm willing to make a PR for it.

Copy link
Contributor

@Sculas Sculas left a comment

Choose a reason for hiding this comment

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

I couldn't find anything out of the ordinary, so LGTM.

Copy link
Member

@ikrivosheev ikrivosheev left a comment

Choose a reason for hiding this comment

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

Small comment

src/error.rs Outdated Show resolved Hide resolved
src/error.rs Outdated Show resolved Hide resolved
src/error.rs Outdated Show resolved Hide resolved
src/error.rs Outdated Show resolved Hide resolved
src/error.rs Show resolved Hide resolved
@ikrivosheev
Copy link
Member

Would an error like UniqueConstraintError be out of scope for SeaORM? If not, maybe some other uncommon errors too? Just to make it easier for the user. If it's not out of scope I'm willing to make a PR for it.

This is needed very often when writing applications.

@Sculas
Copy link
Contributor

Sculas commented Sep 7, 2022

This is needed very often when writing applications.

Yeah. I currently have some function that matches the error and checks if it's a unique constraint violation. It would be so much better if you could just check if it's a DbErr::UniqueConstraintViolation, for example.

@billy1624
Copy link
Member

I think UniqueConstraintError can be a variant of RuntimeErr.

@mohs8421
Copy link
Contributor

mohs8421 commented Sep 8, 2022

@Sculas that was my original intention with this discussion and the pull request in general. This pull request is now merely laying the foundation for what you suggest. There will be more after this one is done. See the discussion and my original PR.
But yes effectively speaking it will help a lot to have all those typical database runtime errors be reflected by seaorm, like fk-related errors or deadlocks which might even be circumvented by a retry mechanism.

@tyt2y3
Copy link
Member Author

tyt2y3 commented Sep 13, 2022

Thanks for the suggestion @ikrivosheev

@tyt2y3
Copy link
Member Author

tyt2y3 commented Sep 13, 2022

I think UniqueConstraintError is only applicable to Exec, not Query ?

Copy link
Member

@ikrivosheev ikrivosheev left a comment

Choose a reason for hiding this comment

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

@tyt2y3 LGTM! I think we can create: UniqueConstraintError in other PR.

@Sculas
Copy link
Contributor

Sculas commented Sep 13, 2022

I think UniqueConstraintError is only applicable to Exec, not Query ?

That sqlx error that contains the unique constraint error is currently mapped to Query, so that would need to be changed.

My review in #750 mapped the error to Query: #750 (review) They should be mapped to Exec instead.

Copy link
Contributor

@Sculas Sculas left a comment

Choose a reason for hiding this comment

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

LGTM. I agree with ikrivosheev.

@mohs8421
Copy link
Contributor

shortly regarding the name of errors:
I think in UniqueConstraintError the Error suffix seems to be noisy in my opinion, as it is already within the DbErr enum. I would rather rename it to UniqueConstraintViolation as that is more speaking.

@billy1624
Copy link
Member

billy1624 commented Sep 14, 2022

I think UniqueConstraintError is only applicable to Exec, not Query ?

It should be applicable to Exec only. But I suspect update / insert operation with RETURNING is executed with query_one. So, the error might end up in DbErr::Query.

@Sculas
Copy link
Contributor

Sculas commented Sep 27, 2022

Hey, any updates on this PR?

@tyt2y3 tyt2y3 merged commit 5d752e6 into master Oct 6, 2022
@tyt2y3 tyt2y3 deleted the better-errors branch October 6, 2022 16:24
@tyt2y3
Copy link
Member Author

tyt2y3 commented Oct 6, 2022

Thank you for your patience

billy1624 added a commit to SeaQL/seaql.github.io that referenced this pull request Oct 31, 2022
tyt2y3 added a commit to SeaQL/seaql.github.io that referenced this pull request Nov 1, 2022
* What's new in SeaORM 0.10.1

* Edit

* Error handling (SeaQL/sea-orm#1002)

* Support array datatype for Postgres (SeaQL/sea-orm#1132)

* generate index file as `lib.rs` instead of `mod.rs` (SeaQL/sea-orm#953)

* Running migration on any Postgres schema (SeaQL/sea-orm#1056)

* More connection options (SeaQL/sea-orm#897, SeaQL/sea-orm#1056)

* Implements `TryFrom<ActiveModel>` for `Model` (SeaQL/sea-orm#990)

* Delete 2022-10-28-whats-new-in-0.10.1.md

Co-authored-by: Chris Tsang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants