this post was submitted on 19 Nov 2024
7 points (100.0% liked)

Rust Programming

8188 readers
2 users here now

founded 5 years ago
MODERATORS
 

I have two functions that are similar but can fail with different errors:

#[derive(Debug, thiserror::Error)]
enum MyError {
  #[error("error a")]
  MyErrorA,
  #[error("error b")]
  MyErrorB,
  #[error("bad value ({0})")]
  MyErrorCommon(String),
}

fn functionA() -> Result<String, MyError> {
  // can fail with MyErrorA MyErrorCommon
  todo!()
}

fn functionB() -> Result<String, MyError> {
  // can fail with MyErrorB MyErrorCommon
  todo!()
}

Is there an elegant (*) way I can express this?

If I split the error type into two separate types, is there a way to reuse the definition of MyErrorCommon?


(*) by "elegant" I mean something that improves the code - I'm sure one could define a few macros and solve that way, but I don't want to go there

edit: grammar (rust grammar)

top 4 comments
sorted by: hot top controversial new old
[–] [email protected] 6 points 3 days ago

You can't create a subset of an enum directly, but splitting this up into multiple types works. You can have FunctionAError with errors that function can produce and a variant for your common errors, and FunctionBError which is similar:

#[derive(Debug, Error)]
enum MyErrorCommon {
    #[error("bad value ({0})")]
    MyErrorCommon(String),
}

#[derive(Debug, Error)]
enum FunctionAError {
    #[error("error a")]
    MyErrorA,
    Common(#[from] MyErrorCommon),
}

// and same for FunctionBError

The try operator (?) will automatically use From impls to convert errors for you as well. If a function returns a result containing MyErrorCommon in your function and you use ? on it, it gets converted to that function's error type for you. thiserror generates the From impl for you if you use #[from].

[–] BB_C 2 points 3 days ago (1 children)
[–] [email protected] 1 points 1 day ago (1 children)

Different functions whose possible failure reasons have a non-empty intersection, but don't coincide completely (IDK if this clarifies? I think the example code in the OP is clearer)

[–] BB_C 1 points 23 hours ago

Yeah, I got that.

I'm asking what would be the benefit of not using a single error enum for all failure reasons?