From abe45434205a48315cf1e24787f1d98f7ec8cd6b Mon Sep 17 00:00:00 2001 From: andrusha Date: Thu, 22 Feb 2024 00:00:44 +0100 Subject: [PATCH] Introduce auth error subtype (#21) --- snowflake-api/src/responses.rs | 15 +++++++++++---- snowflake-api/src/session.rs | 8 +++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/snowflake-api/src/responses.rs b/snowflake-api/src/responses.rs index 6c2bdd5..dee26c3 100644 --- a/snowflake-api/src/responses.rs +++ b/snowflake-api/src/responses.rs @@ -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? @@ -20,7 +20,7 @@ pub enum AuthResponse { Auth(AuthenticatorResponse), Renew(RenewSessionResponse), Close(CloseSessionResponse), - Error(ErrorResponse), + Error(AuthErrorResponse), } #[derive(Deserialize, Debug)] @@ -34,7 +34,8 @@ pub struct BaseRestResponse { pub type PutGetExecResponse = BaseRestResponse; pub type QueryExecResponse = BaseRestResponse; -pub type ErrorResponse = BaseRestResponse; +pub type ExecErrorResponse = BaseRestResponse; +pub type AuthErrorResponse = BaseRestResponse; pub type AuthenticatorResponse = BaseRestResponse; pub type LoginResponse = BaseRestResponse; pub type RenewSessionResponse = BaseRestResponse; @@ -43,7 +44,7 @@ pub type CloseSessionResponse = BaseRestResponse>; #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] -pub struct ErrorResponseData { +pub struct ExecErrorResponseData { pub age: i64, pub error_code: String, pub internal_error: bool, @@ -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, diff --git a/snowflake-api/src/session.rs b/snowflake-api/src/session.rs index b35f935..7567820 100644 --- a/snowflake-api/src/session.rs +++ b/snowflake-api/src/session.rs @@ -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), @@ -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), @@ -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), @@ -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),