Hi, Im continue encapsulate HTTP client base on reqwest. Now I wanna return Result<Option<Record>, Box<dyn Error>> type for different API with a common error type Box<dyn Error>. Everything is ok until Im tring to return some API error message:
async fn find_record(
client: &Client,
base_url: &Url,
username: &str,
token: &str,
domain: &str,
record_host: Option<&str>,
) -> Result<Option<Record>, Box<dyn Error>> {
let url = base_url.join(&format!("{}/records", domain))?;
let response = client
.get(url)
.basic_auth(username, Some(token))
.send()
.await?;
println!("GET {} {}", response.url(), response.status());
if let Err(e) = response.error_for_status_ref() {
let api_error = response.json::<ApiError>().await?;
// Can I combine the two lines into one? I have tried turbofish but it is not working.
let ret: Box<dyn Error> =
format!("API response error: {}", error(api_error.message)).into();
return Err(ret);
}
// more code ...
}
Q1: What's the right way to implement the code I commented?
THX for any help.
Here is the error message I meet when I trying to use turbofish:
error[E0107]: wrong number of type arguments: expected 0, found 1
--> src/dns_provider/name_com.rs:88:83
|
88 | return format!("API response error: {}", error(api_error.message)).into::<Box<dyn Error>>()
| ^^^^^^^^^^^^^^ unexpected type argument
error[E0277]: the trait bound `std::result::Result<std::option::Option<dns_provider::name_com::Record>, std::boxed::Box<dyn std::error::Error>>: std::convert::From<std::string::String>` is not satisfied
--> src/dns_provider/name_com.rs:88:76
|
88 | return format!("API response error: {}", error(api_error.message)).into::<Box<dyn Error>>()
| ^^^^ the trait `std::convert::From<std::string::String>` is not implemented for `std::result::Result<std::option::Option<dns_provider::name_com::Record>, std::boxed::Box<dyn std::error::Error>>`
|
= note: required because of the requirements on the impl of `std::convert::Into<std::result::Result<std::option::Option<dns_provider::name_com::Record>, std::boxed::Box<dyn std::error::Error>>>` for `std::string::String`
After reading it again I find it seems just because into() do not accept type arguments. ![]()