Parse two errors in async function with reqwest

pub async fn get_mining_info(&self) -> Result<MiningInfoResponse, FetchError> {

        let res = self.inner

            .get(self.uri_for("burst"))

            .headers((*self.headers).clone())

            .query(&GetMiningInfoRequest {

                request_type: &"getMiningInfo",

            })

            .send()

            .await

            .map_err(|e| FetchError::from(e))?

            .json::<MiningInfoResponse>()

            .await

            .map_err(|e| FetchError::from(e));

        res

    }

#[derive(Debug)]

pub enum FetchError {

    Http(reqwest::Error),

    Pool(PoolError),

}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PoolErrorWrapper {
    error: PoolError,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PoolError {
    pub code: i32,
    pub message: String,
}

i am confused with the return result.
there are two errors which one is failed to send http with reqwest::Error
and i another get wrong http back.
but in async i don't know how to parse the second error.
can somebody help me ?

Where is PoolError defined? And which function call do you expect to return PoolError on error?

Please properly format your code. You can find a guide here. Don't make every second line empty.

1 Like

hi , i have change my code, can you help me ?

It doesn't look like you've implemented the From trait for FetchError to get that function to compile change FetchError::from(e) to FetchError::Http. Since both of those function calls that you're using map_err on return Result<T, request::Error> they can both map to FetchError::Http

impl From<reqwest::Error> for FetchError {

    fn from(err: reqwest::Error) -> FetchError {

        FetchError::Http(err)

    }

}

impl From<PoolError> for FetchError {

    fn from(err: PoolError) -> FetchError {

        FetchError::Pool(err)

    }

}

it will be two error type. one is http error it should be parse with Http(err)
but another is get the wrong info such as
{msg:"you don't send correct message", code: 400}
but i don't know how to realize these.

so the result data can be also parsed with

 json::<MiningInfoResponse>() 

or

json::<PoolError >() 

so i am so confused

Based on my understanding your question, does something like this get you closer

#[derive(Deserialize)]
#[serde(untagged)]
enum JsonResult {
    Success(MiningInfoResponse),
    Error(PoolError)
}

impl From<JsonResult> for Result<MiningInfoResponse,FetchError> {
    fn from(res: JsonResult) -> Self {
        match res {
            Success(m) => Ok(m),
            Error(e) => Err(FetchError::PoolError(e))
        }
    }
}

and then replace

.json::<MiningInfoResponse>()

with

.json::<JsonResult>().await?.into()

I haven't tried it but I think it should work if MiningInfoResponse and PoolError are defined correctly based on the response of the site.

Please do as alice requested. It's very unpleasant to read your code with all the undesired interspersed blank lines. Most Rustaceans may have a reaction like mine: I ignore code in posts when the poster has previously been asked to format it correctly but continues to not do so.

1 Like
#[derive(Debug, Deserialize)]

#[serde(rename_all = "camelCase")]

struct PoolErrorWrapper {

    error: PoolError,

}

#[derive(Deserialize)]

#[serde(untagged)]

enum JsonResult {

    Success(MiningInfoResponse),

    Error(PoolError),

}

impl From<JsonResult> for Result<MiningInfoResponse, FetchError> {

    fn from(res: JsonResult) -> Self {

        match res {

            JsonResult::Success(m) => Ok(m),

            JsonResult::Error(e) => {

                let err = serde_json::from_slice::<PoolErrorWrapper>(e)?;

                Err(FetchError::Pool(err.error))

            }

        }

    }

}

hi , there are some errors , can you help me ?
or is there any way to solve or realize this function?
when parsing force MiningInfoResponse error,
i need to parse it to PoolErrorWrapper,
and use its x.error as the final return type.

the trait `std::convert::From<serde_json::error::Error>` is not implemented for `wallet::api::FetchError`