tonic-web-wasm-client icon indicating copy to clipboard operation
tonic-web-wasm-client copied to clipboard

error.code() and error handling

Open npcnuno opened this issue 2 years ago • 6 comments

I can't get the error code from the backend, making error handling difficult on the client

I always get code = "Unknow error" when I try to get the error code from the backend

#[wasm_bindgen] pub async fn refresh_token(refresh: String) -> Result<JsValue, JsValue> { let mut client = AuthenticationClient::new(build_client()); let result = client .refresh_token_admin(RefreshRequest { refresh_token: refresh, }) .await;

match result {
    Ok(response) => {
        let res = response.into_inner();
        Ok(JsValue::from_str(
            &serde_json::json!({
            "code" : "0",
            "description": res.auth_token.to_string()})
            .to_string(),
        ))
    }
    Err(error) => Err(JsValue::from_str(
        &serde_json::json!({
            "code": error.code().to_string(),
            "description": error.details(),
        })
        .to_string(),
    )),
}

}

npcnuno avatar Feb 15 '24 05:02 npcnuno

Hi. The error code depends on the code that is returned by the server. Can you share some of your server code here?

devashishdxt avatar Feb 24 '24 06:02 devashishdxt

let admin: Option<Admin> = match admin.unwrap().take(0) {
    Ok(a) => a,
    Err(_) => return Err(Status::internal("couldn't query db")),
};
if admin.is_none() {
    return Err(Status::internal("couldn't query db"));
}
let admin = admin.unwrap();
match create_jwt_auth(
    &decoded.uid,
    &admin.institution.id.to_string(),
    Previlege::from_str(&head.typ.unwrap()).unwrap(),
) {
    Ok(a) => Ok(Response::new(RefreshResponse { auth_token: a })),
    Err(_) => Err(Status::internal("couldn't create auth from refresh")),
}

}

npcnuno avatar Mar 11 '24 15:03 npcnuno

Hi. This is already tested in the test cases: https://github.com/devashishdxt/tonic-web-wasm-client/blob/c89a136c1095c5a242334879cb3fdb32965ca04c/test-suite/simple/client/tests/web.rs#L79

I cannot suggest any meaningful solution for this unless I have a failing test full code which does not behave correctly.

devashishdxt avatar Mar 11 '24 15:03 devashishdxt

Hi, I'm having the same issue. However, I only started encountering the issue once I swapped the traefik reverse proxy that handled grpc-web translation with the tonic_web crate.

I'm guessing this is an issue with tonic_web then. @npcnuno, is your server using that crate as well?

cyanic-selkie avatar Mar 26 '24 20:03 cyanic-selkie

Yes @cyanic-selkie

npcnuno avatar Mar 26 '24 20:03 npcnuno

I opened an issue on the tonic repository.

cyanic-selkie avatar Mar 27 '24 06:03 cyanic-selkie

Closing this issue

devashishdxt avatar May 16 '24 03:05 devashishdxt

Hi, I'm having the same issue. However, I only started encountering the issue once I swapped the traefik reverse proxy that handled grpc-web translation with the tonic_web crate.

I'm guessing this is an issue with tonic_web then. @npcnuno, is your server using that crate as well?

I have the same issue when I use tonic gprc server behind the nginx proxy

imotai avatar Aug 07 '24 05:08 imotai