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

Make Encode return a result #3126

Merged
merged 18 commits into from
May 31, 2024
Merged

Conversation

FSMaxB
Copy link
Contributor

@FSMaxB FSMaxB commented Mar 16, 2024

fixes #2730

Still working on this, but wanted to make a draft PR where I can push my current progress.

My approach was to incrementally turn return values into Result in one layer, but keeping the panic by calling .expect(...) on the layers that use those, then go to the next layer and turn the .expect(...) into ? and push the panics up further and further until they're gone.

I'll leave some open questions as review comments.

@FSMaxB FSMaxB force-pushed the fallible-encode branch 3 times, most recently from 9dde82f to 4a5a6bd Compare March 16, 2024 14:55
@FSMaxB
Copy link
Contributor Author

FSMaxB commented Mar 17, 2024

Getting the macros to work again is really tricky as it turns out!

@FSMaxB FSMaxB force-pushed the fallible-encode branch 7 times, most recently from e831255 to c9226b1 Compare April 10, 2024 11:06
@FSMaxB FSMaxB marked this pull request as ready for review April 10, 2024 11:19
Copy link
Contributor Author

@FSMaxB FSMaxB left a comment

Choose a reason for hiding this comment

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

Leaving some comments and open questions.

sqlx-core/src/any/arguments.rs Show resolved Hide resolved
sqlx-core/src/any/connection/executor.rs Outdated Show resolved Hide resolved
sqlx-core/src/executor.rs Outdated Show resolved Hide resolved
sqlx-core/src/query.rs Show resolved Hide resolved
sqlx-core/src/query.rs Show resolved Hide resolved
sqlx-core/src/query.rs Outdated Show resolved Hide resolved
sqlx-core/src/types/json.rs Show resolved Hide resolved
sqlx-macros-core/src/query/args.rs Show resolved Hide resolved
sqlx-mysql/src/connection/executor.rs Outdated Show resolved Hide resolved
sqlx-postgres/src/types/interval.rs Show resolved Hide resolved
sqlx-core/src/query.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@abonander abonander left a comment

Choose a reason for hiding this comment

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

In general, we need to be careful not to leave argument buffers in an invalid state on error, in case someone decides to retry.

@FSMaxB FSMaxB force-pushed the fallible-encode branch 5 times, most recently from c502a9a to a1074be Compare April 24, 2024 21:23
This only changes the trait for now and makes it compile, calling .expect() on all users. Those will be removed in a later commit.
This pushes the panics one level further to mostly bind calls. Those will also be removed later.
@FSMaxB FSMaxB force-pushed the fallible-encode branch 2 times, most recently from 7de461a to d73561f Compare April 26, 2024 16:21
@FSMaxB
Copy link
Contributor Author

FSMaxB commented Apr 26, 2024

@abonander I believe I have addressed all review comments, so this is ready for another round of review.

Copy link
Collaborator

@abonander abonander left a comment

Choose a reason for hiding this comment

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

Looks good with one final nit.

sqlx-mysql/src/protocol/statement/execute.rs Outdated Show resolved Hide resolved
sqlx-mysql/src/types/json.rs Show resolved Hide resolved
@abonander abonander merged commit c57b46c into launchbadge:main May 31, 2024
4 checks passed
@FSMaxB FSMaxB deleted the fallible-encode branch May 31, 2024 21:24
jayy-lmao pushed a commit to jayy-lmao/sqlx that referenced this pull request Jun 6, 2024
* Make encode and encode_by_ref fallible

This only changes the trait for now and makes it compile, calling .expect() on all users. Those will be removed in a later commit.

* PgNumeric: Turn TryFrom Decimal to an infallible From

* Turn panics in Encode implementations into errors

* Add Encode error analogous to the Decode error

* Propagate decode errors through Arguments::add

This pushes the panics one level further to mostly bind calls. Those will also be removed later.

* Only check argument encoding at the end

* Use Result in Query internally

* Implement query_with functions in terms of _with_result

* Surface encode errors when executing a query.

* Remove remaining panics in AnyConnectionBackend implementations

* PostgreSQL BigDecimal: Return encode error immediately

* Arguments: Add len method to report how many arguments were added

* Query::bind: Report which argument failed to encode

* IsNull: Add is_null method

* MySqlArguments: Replace manual bitmap code with NullBitMap helper type

* Roll back buffer in MySqlArguments if encoding fails

* Roll back buffer in SqliteArguments if encoding fails

* Roll back PgArgumentBuffer if encoding fails
jrasanen pushed a commit to jrasanen/sqlx that referenced this pull request Oct 14, 2024
* Make encode and encode_by_ref fallible

This only changes the trait for now and makes it compile, calling .expect() on all users. Those will be removed in a later commit.

* PgNumeric: Turn TryFrom Decimal to an infallible From

* Turn panics in Encode implementations into errors

* Add Encode error analogous to the Decode error

* Propagate decode errors through Arguments::add

This pushes the panics one level further to mostly bind calls. Those will also be removed later.

* Only check argument encoding at the end

* Use Result in Query internally

* Implement query_with functions in terms of _with_result

* Surface encode errors when executing a query.

* Remove remaining panics in AnyConnectionBackend implementations

* PostgreSQL BigDecimal: Return encode error immediately

* Arguments: Add len method to report how many arguments were added

* Query::bind: Report which argument failed to encode

* IsNull: Add is_null method

* MySqlArguments: Replace manual bitmap code with NullBitMap helper type

* Roll back buffer in MySqlArguments if encoding fails

* Roll back buffer in SqliteArguments if encoding fails

* Roll back PgArgumentBuffer if encoding fails
jrasanen pushed a commit to jrasanen/sqlx that referenced this pull request Oct 14, 2024
* Make encode and encode_by_ref fallible

This only changes the trait for now and makes it compile, calling .expect() on all users. Those will be removed in a later commit.

* PgNumeric: Turn TryFrom Decimal to an infallible From

* Turn panics in Encode implementations into errors

* Add Encode error analogous to the Decode error

* Propagate decode errors through Arguments::add

This pushes the panics one level further to mostly bind calls. Those will also be removed later.

* Only check argument encoding at the end

* Use Result in Query internally

* Implement query_with functions in terms of _with_result

* Surface encode errors when executing a query.

* Remove remaining panics in AnyConnectionBackend implementations

* PostgreSQL BigDecimal: Return encode error immediately

* Arguments: Add len method to report how many arguments were added

* Query::bind: Report which argument failed to encode

* IsNull: Add is_null method

* MySqlArguments: Replace manual bitmap code with NullBitMap helper type

* Roll back buffer in MySqlArguments if encoding fails

* Roll back buffer in SqliteArguments if encoding fails

* Roll back PgArgumentBuffer if encoding fails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSON serialization error panics with PostgreSQL
3 participants