0

I have a struct with some functions and when something goes wrong I call a function fn set_error(&self,msg:&str) to log the error and possible show an error message etc.

Currently this is very simple and I use it like following in other functions:

pub fn somefunction(&self) {
  ...some code...
  if error == true {
    self.set_error("The error message");
    Err("The error message")
  }else{
    Ok("All ok")
  }
}

To begin with it could be useful for the set_error()-function to return a Result::Error object in some way so calling set_error() would be the same as:

  self.set_error("The error message");
  Err("The error message")

Another way could be to return the error-string again from the set_error()-function to be able to do:

  Err( self.set_error("The error message") )

Also, is there a simple/nice way to join/concat strings (String, &str etc) that is sent to the set_error()-function? For example, in match-Err there could be a std::io::error and maybe a u32 where I would like to do:

  self.set_error("A lvl 10 Error occured: " +  err.to_string() + ". UserId:" + user_id.to_string() + ". Some other info...");

This thought is taken from PHP where that can be done to create the error string in the call to the function in a simple way.

How would a set_error()-function look to handle error in my scenario and how would I build an error-message, sent to set_error(), in a good way?


More info:

Doing self.set_error(format!("..Error..: {err}. UserId:{user_id}.")), as suggested by user4815162342, results in problems when trying to do: return Err( self.set_error(format!("..Error..: {err}. UserId:{user_id}.")) );. This is since Err() need a &str and if I change self.set_error to return &str then that &str only lives in the function and can't be used outside it. If trying to do

fn set_error(&self,msg:String) -> String {
  println!("Err: {msg}");
  msg
}
...
return Err(self.set_error(format!("..Error..: {err}. UserId:{user_id}.").as_str());
...

Then I get:

cannot return value referencing temporary value.
returns a value referencing data owned by the current function.

So how would the set_error()-function look to be able to do: return Err(self.set_error(format!("..Error..: {err}. UserId:{user_id}.")); or similar? I'm not looking to get exactly this to work but to get something easy to use to work.

4
  • 1
    I guess set_error() would receive a msg: impl Into<String>. That way you can call it with a string literal, as before, but also with a dynamically generated string, which you create with the usual format!(), e.g.: self.set_error(format!("A lvl 10 Error occurred: {err}. UserId:{user_id}. Some other info...")). Convenience level comparable to PHP. Commented Jan 7, 2024 at 21:48
  • 1
    Can you explain where exactly you're stuck? All of your possibilities seem relatively straight forward to implement with the tip from user4815162342. Commented Jan 7, 2024 at 22:03
  • @cafce25 See my edited post about where I'm stuck. Commented Jan 13, 2024 at 23:44
  • If you might have to dynamically create the error it should be a String, not a &str. Commented Jan 14, 2024 at 7:29

1 Answer 1

-1

You can use the format macro to concatenate the error strings.

format!("{}\n{}", previous_error, new_error);

Or vice versa

Sign up to request clarification or add additional context in comments.

2 Comments

That will fail to borrow-check with the OP's definition of set_error(), which accepts a &str.
You did this: return Err(self.set_error(format!("..Error..: {err}. UserId:{user_id}.").as_str()); But the correct anwser is: return Err(self.set_error(format!("..Error..: {err}. UserId:{user_id}.")));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.