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

Thoroughly fix the error handling #22

Merged
merged 9 commits into from Aug 3, 2017
Merged

Thoroughly fix the error handling #22

merged 9 commits into from Aug 3, 2017

Conversation

kw217
Copy link
Collaborator

@kw217 kw217 commented Jul 27, 2017

This is a thorough root-and-branch replacement of all the error handling with something sane.

There is now a single error type defined in errors.rs, which has variants for plain Cassandra errors (just a return code), detailed errors (with additional info), internal errors (currently only "unexpected type") or various foreign errors (in particular, problems converting strings to/from C).

This error type is used appropriately everywhere, and has appropriate conversions as necessary.

In addition I've audited and removed all (well, almost all) uses of unwrap, expect, panic!, unimplemented, and friends, replacing them with principled use of ? and Result. There are a few that remain where they are appropriate, and one (Statement::new) I'm not 100% happy with but think it best to leave.

This is very much a breaking API change - several return types have been changed (from T to Result<T, Error>, or from Result<T, WrongError> to Result<T, Error>, usually, though some other changes have been made too).

Some other choice details:

  • Enhance the enhance_nullary_enum macro to cope with omitted variants. We don't want CASS_OK or CASS_ERROR_LAST_ENTRY to count as errors which you need to consider in match; we should ensure these just never happen.
  • Move some error-handling code from futures to error module. Add some other nice error building helpers. Remove direct tests of CASS_OK elsewhere and depend on helpers in error module.
  • Define WriteType and ValueType as nice modern enums.
  • Fix cassandra_sys in Tidy API and bump version cassandra-sys-rs#7 to not attempt to add limited error-chain support to the bare return code; instead use an extension trait in this crate to do it (adding to_result). Also add some missing symbols while we're at it.
  • Get rid of all uses of .chain_err(|| "pointless text"); the Cassandra errors speak for themselves and don't need the addition of an extra wrapper.

Enjoy!

* Remove references to the old approach.
* Define the outline of the new approach.
* Apply it everywhere.
* Move some error-handling code from futures to error.
* Define an injection for FFI NulError.
* Define WriteType as a nice modern enum.
* Enhance the enhance_nullary_enum macro to cope with omitted variants.

Signed-off-by: Keith Wansbrough <Keith.Wansbrough@metaswitch.com>
Signed-off-by: Keith Wansbrough <Keith.Wansbrough@metaswitch.com>
This has new names for the missing symbols in error-result.

It also removes the to_result / error-chain handling; that rightfully
belongs in this crate not there, and it was polluting the namespace.

Signed-off-by: Keith Wansbrough <Keith.Wansbrough@metaswitch.com>
Signed-off-by: Keith Wansbrough <Keith.Wansbrough@metaswitch.com>
Cassandra errors are now just reported as such; they don't get string wrappers.

Signed-off-by: Keith Wansbrough <Keith.Wansbrough@metaswitch.com>
Signed-off-by: Keith Wansbrough <Keith.Wansbrough@metaswitch.com>

* Remove almost all uses of expect, unwrap, panic, unimplemented, etc.
* Correct a few cases where the wrong error was being reported. All errors should now be cassandra::Errors.
* Improvements to error construction helpers.
* Wrap the ValueType type.
Signed-off-by: Keith Wansbrough <Keith.Wansbrough@metaswitch.com>
Signed-off-by: Keith Wansbrough <Keith.Wansbrough@metaswitch.com>
@kw217
Copy link
Collaborator Author

kw217 commented Jul 27, 2017

(now rebased with correct signed-off-by lines!)

@@ -124,22 +119,37 @@ impl Debug for Column {
CASS_VALUE_TYPE_TIMEUUID => write!(f, "TIMEUUID Cassandra type"),
CASS_VALUE_TYPE_INET => write!(f, "INET Cassandra type"),
CASS_VALUE_TYPE_LIST => {
for item in self.set_iter().expect("a list should always be able to return a set iterator") {
write!(f, "LIST {:?}", item)?
match self.set_iter() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Don't worry too much about these - I make a better fix to them in the next PR, #23

cass_result_column_name(self.0, index, name, name_length);
let slice = slice::from_raw_parts(name as *const u8, name_length as usize);
str::from_utf8(slice).expect("must be utf8").to_owned()
cass_result_column_name(self.0, index, name, name_length).to_result(())
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Spot the bug - fixed in #23

Copy link
Contributor

Choose a reason for hiding this comment

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

Surely the real solution is to move everything into Rust and stop using Unsafe code? 😈

@kw217 kw217 mentioned this pull request Jul 27, 2017
@kw217
Copy link
Collaborator Author

kw217 commented Jul 27, 2017

Note to self - fix Cargo.toml to point to the new 0.10 library once it's merged.

@kw217 kw217 removed the request for review from johnbatty August 2, 2017 14:37
Copy link
Contributor

@angusi angusi left a comment

Choose a reason for hiding this comment

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

With the usual caveat surrounding me reviewing the C* FFI, this looks pretty good. I like just how negatively productive you seem to have been! 😉 Thanks

@angusi angusi assigned kw217 and unassigned angusi Aug 2, 2017
@angusi
Copy link
Contributor

angusi commented Aug 2, 2017

Oh - do we also need a version bump, if we've an API breaking change?

@kw217
Copy link
Collaborator Author

kw217 commented Aug 2, 2017

I've not yet released the previous breaking change. I'm holding off so I can release them all together, as v0.9.0. (hence this is -pre1). I guess technically I could have bumped to -pre2? But it doesn't really matter at this stage, especially since versions are ignored by Cargo on git dependencies.

@kw217
Copy link
Collaborator Author

kw217 commented Aug 2, 2017

Thanks Angus.

TODO: depend on updated cassandra-sys crate, then merge.

Signed-off-by: Keith Wansbrough <Keith.Wansbrough@metaswitch.com>
@kw217 kw217 merged commit a1f1660 into master Aug 3, 2017
@kw217 kw217 deleted the fix-errors branch June 19, 2018 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants