Skip to content

Commit

Permalink
Introduce auth error subtype (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrusha authored Feb 21, 2024
1 parent ebaa93a commit abe4543
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 11 additions & 4 deletions snowflake-api/src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::Deserialize;
pub enum ExecResponse {
Query(QueryExecResponse),
PutGet(PutGetExecResponse),
Error(ErrorResponse),
Error(ExecErrorResponse),
}

// todo: add close session response, which should be just empty?
Expand All @@ -20,7 +20,7 @@ pub enum AuthResponse {
Auth(AuthenticatorResponse),
Renew(RenewSessionResponse),
Close(CloseSessionResponse),
Error(ErrorResponse),
Error(AuthErrorResponse),
}

#[derive(Deserialize, Debug)]
Expand All @@ -34,7 +34,8 @@ pub struct BaseRestResponse<D> {

pub type PutGetExecResponse = BaseRestResponse<PutGetResponseData>;
pub type QueryExecResponse = BaseRestResponse<QueryExecResponseData>;
pub type ErrorResponse = BaseRestResponse<ErrorResponseData>;
pub type ExecErrorResponse = BaseRestResponse<ExecErrorResponseData>;
pub type AuthErrorResponse = BaseRestResponse<AuthErrorResponseData>;
pub type AuthenticatorResponse = BaseRestResponse<AuthenticatorResponseData>;
pub type LoginResponse = BaseRestResponse<LoginResponseData>;
pub type RenewSessionResponse = BaseRestResponse<RenewSessionResponseData>;
Expand All @@ -43,7 +44,7 @@ pub type CloseSessionResponse = BaseRestResponse<Option<()>>;

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ErrorResponseData {
pub struct ExecErrorResponseData {
pub age: i64,
pub error_code: String,
pub internal_error: bool,
Expand All @@ -57,6 +58,12 @@ pub struct ErrorResponseData {
pub sql_state: String,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct AuthErrorResponseData {
pub authn_method: String,
}

#[derive(Deserialize, Debug)]
pub struct NameValueParameter {
pub name: String,
Expand Down
8 changes: 5 additions & 3 deletions snowflake-api/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum AuthError {
#[error("Unexpected API response")]
UnexpectedResponse,

// todo: add code mapping to meaningful message and/or refer to docs
// eg https://docs.snowflake.com/en/user-guide/key-pair-auth-troubleshooting
#[error("Failed to authenticate. Error code: {0}. Message: {1}")]
AuthFailed(String, String),

Expand Down Expand Up @@ -261,7 +263,7 @@ impl Session {
match resp {
AuthResponse::Close(_) => Ok(()),
AuthResponse::Error(e) => Err(AuthError::AuthFailed(
e.data.error_code,
e.code.unwrap_or_default(),
e.message.unwrap_or_default(),
)),
_ => Err(AuthError::UnexpectedResponse),
Expand Down Expand Up @@ -348,7 +350,7 @@ impl Session {
})
}
AuthResponse::Error(e) => Err(AuthError::AuthFailed(
e.data.error_code,
e.code.unwrap_or_default(),
e.message.unwrap_or_default(),
)),
_ => Err(AuthError::UnexpectedResponse),
Expand Down Expand Up @@ -409,7 +411,7 @@ impl Session {
})
}
AuthResponse::Error(e) => Err(AuthError::AuthFailed(
e.data.error_code,
e.code.unwrap_or_default(),
e.message.unwrap_or_default(),
)),
_ => Err(AuthError::UnexpectedResponse),
Expand Down

0 comments on commit abe4543

Please sign in to comment.