async fn new_api_error(res: Response) -> Error {
    let status = res.status();
    match status {
        StatusCode::BAD_REQUEST
        | StatusCode::NOT_FOUND
        | StatusCode::TOO_MANY_REQUESTS
        | StatusCode::FORBIDDEN => {
            let error = res.json::<ApiErrors>().await;
            match error {
                Err(parse_err) => parse_err
                    .context(ErrorKind::ApiMalformedJSON)
                    .context(ErrorKind::ApiError(status))
                    .into(),
                Ok(errors) => {
                    // TODO: provide better error messages if errors are valid JSON
                    errors.context(ErrorKind::ApiError(status)).into()
                }
            }
        }
        _ => {
            debug!("Unexpected response: {:#?}", res);
            debug!("Unexpected response content: {:#?}", res.text().await);
            ErrorKind::ApiUnexpectedStatus(status).into()
        }
    }
}
/// Given a response that _should_ contain a well-structured ApiErrors JSON
/// value, returns a descriptive Error about what went wrong
Given a response that _should_ contain a well-structured ApiErrors JSON
value, returns a descriptive Error about what went wrong
Given a response that _should_ contain a well - structured ApiErrors JSON value returns a descriptive Error about what went wrong
async fn new_api_error(res: Response) -> Error {
    let status = res.status();
    match status {
        StatusCode::BAD_REQUEST
        | StatusCode::NOT_FOUND
        | StatusCode::TOO_MANY_REQUESTS
        | StatusCode::FORBIDDEN => {
            let error = res.json::<ApiErrors>().await;
            match error {
                Err(parse_err) => parse_err
                    .context(ErrorKind::ApiMalformedJSON)
                    .context(ErrorKind::ApiError(status))
                    .into(),
                Ok(errors) => {
                    // TODO: provide better error messages if errors are valid JSON
                    errors.context(ErrorKind::ApiError(status)).into()
                }
            }
        }
        _ => {
            debug!("Unexpected response: {:#?}", res);
            debug!("Unexpected response content: {:#?}", res.text().await);
            ErrorKind::ApiUnexpectedStatus(status).into()
        }
    }
}
async fn new_api_error ( res : Response ) -> Error { let status = res . status ( ) ; match status { StatusCode :: BAD_REQUEST | StatusCode :: NOT_FOUND | StatusCode :: TOO_MANY_REQUESTS | StatusCode :: FORBIDDEN => { let error = res . json :: < ApiErrors > ( ) . await ; match error { Err ( parse_err ) => parse_err . context ( ErrorKind :: ApiMalformedJSON ) . context ( ErrorKind :: ApiError ( status ) ) . into ( ) , Ok ( errors ) => { // TODO: provide better error messages if errors are valid JSON errors . context ( ErrorKind :: ApiError ( status ) ) . into ( ) } } } _ => { debug ! ( " " res ) ; debug ! ( " " res text ( ) await ) ; ErrorKind :: ApiUnexpectedStatus ( status ) . into ( ) } } }
Given a response that _should_ contain a well-structured ApiErrors JSON
value, returns a descriptive Error about what went wrong
Given a response that _should_ contain a well - structured ApiErrors JSON value returns a descriptive Error about what went wrong
res
Response
res
Response