Skip to content

Commit

Permalink
Merge pull request #1067 from muzarski/connection_broken_error
Browse files Browse the repository at this point in the history
errors: refactor error types on a connection level
  • Loading branch information
wprzytula authored Sep 18, 2024
2 parents 9ff1161 + 9259dea commit d08018b
Show file tree
Hide file tree
Showing 9 changed files with 820 additions and 254 deletions.
421 changes: 384 additions & 37 deletions scylla-cql/src/errors.rs

Large diffs are not rendered by default.

23 changes: 19 additions & 4 deletions scylla-cql/src/frame/frame_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use super::TryFromPrimitiveError;
use crate::cql_to_rust::CqlTypeError;
use crate::errors::CqlResponseKind;
use crate::frame::value::SerializeValuesError;
use crate::types::deserialize::{DeserializationError, TypeCheckError};
use crate::types::serialize::SerializationError;
Expand Down Expand Up @@ -78,6 +79,20 @@ pub enum CqlResponseParseError {
CqlResultParseError(#[from] CqlResultParseError),
}

impl CqlResponseParseError {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
CqlResponseParseError::CqlErrorParseError(_) => CqlResponseKind::Error,
CqlResponseParseError::CqlAuthChallengeParseError(_) => CqlResponseKind::AuthChallenge,
CqlResponseParseError::CqlAuthSuccessParseError(_) => CqlResponseKind::AuthSuccess,
CqlResponseParseError::CqlAuthenticateParseError(_) => CqlResponseKind::Authenticate,
CqlResponseParseError::CqlSupportedParseError(_) => CqlResponseKind::Supported,
CqlResponseParseError::CqlEventParseError(_) => CqlResponseKind::Event,
CqlResponseParseError::CqlResultParseError(_) => CqlResponseKind::Result,
}
}
}

/// An error type returned when deserialization of ERROR response fails.
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
Expand Down Expand Up @@ -134,15 +149,15 @@ pub enum CqlResultParseError {
ResultIdParseError(LowLevelDeserializationError),
#[error("Unknown RESULT response id: {0}")]
UnknownResultId(i32),
#[error("'Set_keyspace' response deserialization failed: {0}")]
#[error("RESULT:Set_keyspace response deserialization failed: {0}")]
SetKeyspaceParseError(#[from] SetKeyspaceParseError),
// This is an error returned during deserialization of
// `RESULT::Schema_change` response, and not `EVENT` response.
#[error("'Schema_change' response deserialization failed: {0}")]
#[error("RESULT:Schema_change response deserialization failed: {0}")]
SchemaChangeParseError(#[from] SchemaChangeEventParseError),
#[error("'Prepared' response deserialization failed: {0}")]
#[error("RESULT:Prepared response deserialization failed: {0}")]
PreparedParseError(#[from] PreparedParseError),
#[error("'Rows' response deserialization failed: {0}")]
#[error("RESULT:Rows response deserialization failed: {0}")]
RowsParseError(#[from] RowsParseError),
}

Expand Down
41 changes: 36 additions & 5 deletions scylla-cql/src/frame/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::Arc;
pub use error::Error;
pub use supported::Supported;

use crate::errors::QueryError;
use crate::errors::{CqlResponseKind, UserRequestError};
use crate::frame::protocol_features::ProtocolFeatures;
use crate::frame::response::result::ResultMetadata;
use crate::frame::TryFromPrimitiveError;
Expand Down Expand Up @@ -64,6 +64,19 @@ pub enum Response {
}

impl Response {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
Response::Error(_) => CqlResponseKind::Error,
Response::Ready => CqlResponseKind::Ready,
Response::Result(_) => CqlResponseKind::Result,
Response::Authenticate(_) => CqlResponseKind::Authenticate,
Response::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
Response::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
Response::Supported(_) => CqlResponseKind::Supported,
Response::Event(_) => CqlResponseKind::Event,
}
}

pub fn deserialize(
features: &ProtocolFeatures,
opcode: ResponseOpcode,
Expand Down Expand Up @@ -93,17 +106,21 @@ impl Response {
Ok(response)
}

pub fn into_non_error_response(self) -> Result<NonErrorResponse, QueryError> {
Ok(match self {
Response::Error(err) => return Err(QueryError::from(err)),
pub fn into_non_error_response(self) -> Result<NonErrorResponse, UserRequestError> {
let non_error_response = match self {
Response::Error(error::Error { error, reason }) => {
return Err(UserRequestError::DbError(error, reason))
}
Response::Ready => NonErrorResponse::Ready,
Response::Result(res) => NonErrorResponse::Result(res),
Response::Authenticate(auth) => NonErrorResponse::Authenticate(auth),
Response::AuthSuccess(auth_succ) => NonErrorResponse::AuthSuccess(auth_succ),
Response::AuthChallenge(auth_chal) => NonErrorResponse::AuthChallenge(auth_chal),
Response::Supported(sup) => NonErrorResponse::Supported(sup),
Response::Event(eve) => NonErrorResponse::Event(eve),
})
};

Ok(non_error_response)
}
}

Expand All @@ -118,3 +135,17 @@ pub enum NonErrorResponse {
Supported(Supported),
Event(event::Event),
}

impl NonErrorResponse {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
NonErrorResponse::Ready => CqlResponseKind::Ready,
NonErrorResponse::Result(_) => CqlResponseKind::Result,
NonErrorResponse::Authenticate(_) => CqlResponseKind::Authenticate,
NonErrorResponse::AuthSuccess(_) => CqlResponseKind::AuthSuccess,
NonErrorResponse::AuthChallenge(_) => CqlResponseKind::AuthChallenge,
NonErrorResponse::Supported(_) => CqlResponseKind::Supported,
NonErrorResponse::Event(_) => CqlResponseKind::Event,
}
}
}
Loading

0 comments on commit d08018b

Please sign in to comment.