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

Return idiomatic error enums instead of low-level library error codes #7

Closed
AlexTMjugador opened this issue Dec 22, 2022 · 0 comments
Closed
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@AlexTMjugador
Copy link
Member

Currently, the internal macro that converts low-level C library error codes to an user-facing Result just passes through error codes returned by C functions, which users can compare with some crate constants to know the precise error cause:

macro_rules! return_value_to_result {
( $func:ident ( $($arg:expr),* ), $is_ok:expr, $lib_name:literal, $result_to_error_string:expr ) => {{
let return_value = $func($($arg),*) as i32;
if $is_ok(return_value) {
Ok(return_value)
} else {
Err(VorbisError::LibraryError {
__error_message: concat!($lib_name, " error calling ", stringify!($func), ". Reason: ").into(),
__error_reason: $result_to_error_string(return_value).into(),
error_code: return_value
})
}
}};

While this works fine in most cases, it's not an ideal design for several reasons:

  • In theory, the same error code may be used by different C libraries to represent different error causes. Moreover, users can't know which library the error code belongs to, as that information is not meant to be made available on the error struct.
  • A set of error causes is more idiomatically represented in Rust by an enum type, rather than a raw integer.

Therefore, a welcome improvement would be replacing the error_code field with a error_cause field, which relates the library that caused the error with its actual cause. This would complicate the error handling logic significantly, but I think it's worth it for end usage.

@AlexTMjugador AlexTMjugador added enhancement New feature or request good first issue Good for newcomers labels Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant