Skip to content
Snippets Groups Projects
Commit 5c692e7b authored by Georges Racinet's avatar Georges Racinet
Browse files

rhgitaly::errors: conveniences for concrete inner error types.

Made a submodule in order to be able to explain the conventions,
see the doc-comment about that.
parent 09ee0cfa
No related branches found
No related tags found
2 merge requests!245Update MSRV to 1.73 and merge stable,!239RHGitaly GetTreeEntries structured errors
......@@ -46,3 +46,70 @@
gen_details_bytes(code, message, vec![as_any]),
)
}
/// This module provides helpers about the error message types defined in `errors.proto`, for
/// use in instantiation of concrete errors at the service level.
///
/// It should be expanded with more cases as needed.
///
/// ## Features
///
/// ### Enum convenience alias.
///
/// The paths of enums defined in the message types is usually convoluted and cannot be
/// shortened without aliasing by an import. We reexpose them in this module, with just enough
/// aliasing to avoid naming collisions.
///
/// A prime example is [`PathErrorType`].
///
/// ### Factory traits
///
/// These traits are to be implemented on service-level structured error types (which typically
/// are enums). Synopsis:
///
/// ```text
/// pub trait FromSomeError {
/// fn from_some_error(err: SomeError);
///
/// fn some_error(...) -> Self { // blanket implementation using `from_some_error()` }
/// ```
///
/// Service-level code is expected to implement the trait and then probably only use the
/// blanket `some_error` associated function.
///
/// New traits should follow the same namings: putting the emphasis on the method to be
/// implemented is fairly common, but in this case the main advantage is to avoid a naming
/// collision with the error type itself (`SomeError` in the example above). Such collisions
/// are not blockers, as callers can use explicit paths or aliasing, but the point is to make
/// life easier for callers, hence any reducing of the burden is good to be taken.
///
/// [`FromResolveRevisionError`] is a prime example for simple inner error types with scalar
/// fields only.
///
/// [`FromPathError`] is a prime example for inner error types with enum fields, adding the
/// conversion to numeric value to the small conveniences for the callers.
pub mod error_messages {
pub use crate::gitaly::path_error::ErrorType as PathErrorType;
use crate::gitaly::{PathError, ResolveRevisionError};
pub trait FromPathError: Sized {
fn from_path_error(err: PathError) -> Self;
fn path_error(path: Vec<u8>, err_type: PathErrorType) -> Self {
Self::from_path_error(PathError {
path,
error_type: err_type as i32,
})
}
}
pub trait FromResolveRevisionError: Sized {
fn from_resolve_revision_error(err: ResolveRevisionError) -> Self;
fn resolve_revision_error(revision: Vec<u8>) -> Self {
Self::from_resolve_revision_error(ResolveRevisionError { revision })
}
}
}
pub use error_messages::*;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment