Skip to content

Commit c96fdb4

Browse files
authored
fix: improve error quality when vertex oauth exchange fails (#1647)
meant to include this in #1645
1 parent c3b9011 commit c96fdb4

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

  • engine/baml-runtime/src/internal/llm_client/primitive/vertex

engine/baml-runtime/src/internal/llm_client/primitive/vertex/wasm_auth.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,39 @@ impl VertexAuth {
5656
("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer"),
5757
("assertion", &jwt),
5858
];
59-
let res: serde_json::Value = client
59+
let res = client
6060
.post(&self.0.token_uri)
6161
.form(&params)
6262
.send()
6363
.await?
64-
.json()
64+
.text()
6565
.await?;
6666

67-
Ok(Arc::new(Token(
68-
res.as_object()
69-
.context("Token exchange did not return a JSON object")?
70-
.get("access_token")
71-
.context("Access token not found in response")?
72-
.as_str()
73-
.context("Access token is not a string")?
74-
.to_string(),
75-
)))
67+
parse_token_response(&res)
68+
.context(format!("OAuth2 access token request failed: {res}"))
69+
.map(Arc::new)
7670
}
7771

7872
pub async fn project_id(&self) -> Result<String> {
7973
Ok(self.0.project_id.clone())
8074
}
8175
}
8276

77+
fn parse_token_response(response: &str) -> Result<Token> {
78+
let res: serde_json::Value =
79+
serde_json::from_str(response).context("Failed to parse token response as JSON")?;
80+
81+
Ok(Token(
82+
res.as_object()
83+
.context("Token exchange did not return a JSON object")?
84+
.get("access_token")
85+
.context("Access token not found in response")?
86+
.as_str()
87+
.context("Access token is not a string")?
88+
.to_string(),
89+
))
90+
}
91+
8392
#[derive(Debug, Serialize, Deserialize)]
8493
struct Claims {
8594
iss: String,

0 commit comments

Comments
 (0)