Skip to content

Commit

Permalink
RSCBC-19: Rework searchx error model
Browse files Browse the repository at this point in the history
  • Loading branch information
chvck committed Jan 30, 2025
1 parent cc8465c commit 2eddd57
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 216 deletions.
64 changes: 37 additions & 27 deletions sdk/couchbase-core/src/queryx/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use http::StatusCode;
use serde::de::StdError;
use serde_json::Value;
use std::collections::HashMap;
use std::error::Error as StdError;
use std::fmt::{Display, Formatter};
use std::sync::Arc;

Expand All @@ -18,11 +18,20 @@ impl Display for Error {
}
}

impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.inner
.source
.as_ref()
.map(|cause| &**cause as &(dyn StdError + 'static))
}
}

impl Error {
pub(crate) fn new_server_error(e: ServerError) -> Error {
Self {
inner: ErrorImpl {
kind: Box::new(ErrorKind::ServerError(e)),
kind: Box::new(ErrorKind::Server(e)),
source: None,
},
}
Expand Down Expand Up @@ -87,7 +96,7 @@ impl Error {
) -> Self {
Self {
inner: ErrorImpl {
kind: Box::new(ErrorKind::HttpError {
kind: Box::new(ErrorKind::Http {
endpoint: endpoint.into(),
statement: statement.into(),
client_context_id: client_context_id.into(),
Expand All @@ -110,22 +119,23 @@ impl Error {
type Source = Arc<dyn StdError + Send + Sync>;

#[derive(Debug, Clone)]
pub struct ErrorImpl {
struct ErrorImpl {
kind: Box<ErrorKind>,
source: Option<Source>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ErrorKind {
ServerError(ServerError),
Server(ServerError),
#[non_exhaustive]
HttpError {
Http {
endpoint: String,
statement: Option<String>,
client_context_id: Option<String>,
},
Resource(ResourceError),
#[non_exhaustive]
Message {
msg: String,
endpoint: Option<String>,
Expand All @@ -137,6 +147,7 @@ pub enum ErrorKind {
msg: String,
arg: Option<String>,
},
#[non_exhaustive]
Encoding {
msg: String,
},
Expand All @@ -145,7 +156,7 @@ pub enum ErrorKind {
impl Display for ErrorKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ErrorKind::ServerError(e) => write!(f, "{}", e),
ErrorKind::Server(e) => write!(f, "{}", e),
ErrorKind::Resource(e) => write!(f, "{}", e),
ErrorKind::InvalidArgument { msg, arg } => {
let base_msg = format!("invalid argument: {msg}");
Expand All @@ -156,7 +167,7 @@ impl Display for ErrorKind {
}
}
ErrorKind::Encoding { msg } => write!(f, "encoding error: {msg}"),
ErrorKind::HttpError {
ErrorKind::Http {
endpoint,
statement,
client_context_id,
Expand Down Expand Up @@ -193,7 +204,6 @@ impl Display for ErrorKind {
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServerError {
kind: ServerErrorKind,

Expand Down Expand Up @@ -608,7 +618,7 @@ impl Error {
pub fn is_parsing_failure(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::ParsingFailure,
..
})
Expand All @@ -618,7 +628,7 @@ impl Error {
pub fn is_internal(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::Internal,
..
})
Expand All @@ -628,7 +638,7 @@ impl Error {
pub fn is_authentication_failure(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::AuthenticationFailure,
..
})
Expand All @@ -638,7 +648,7 @@ impl Error {
pub fn is_cas_mismatch(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::CasMismatch,
..
})
Expand All @@ -648,7 +658,7 @@ impl Error {
pub fn is_doc_not_found(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::DocNotFound,
..
})
Expand All @@ -658,7 +668,7 @@ impl Error {
pub fn is_doc_exists(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::DocExists,
..
})
Expand All @@ -668,7 +678,7 @@ impl Error {
pub fn is_planning_failure(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::PlanningFailure,
..
})
Expand All @@ -678,7 +688,7 @@ impl Error {
pub fn is_index_failure(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::IndexFailure,
..
})
Expand All @@ -688,7 +698,7 @@ impl Error {
pub fn is_prepared_statement_failure(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::PreparedStatementFailure,
..
})
Expand All @@ -698,7 +708,7 @@ impl Error {
pub fn is_dml_failure(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::DMLFailure,
..
})
Expand All @@ -708,7 +718,7 @@ impl Error {
pub fn is_server_timeout(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::Timeout,
..
})
Expand All @@ -718,7 +728,7 @@ impl Error {
pub fn is_write_in_read_only_mode(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::WriteInReadOnlyMode,
..
})
Expand All @@ -728,7 +738,7 @@ impl Error {
pub fn is_invalid_argument(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::InvalidArgument { .. },
..
})
Expand All @@ -738,7 +748,7 @@ impl Error {
pub fn is_build_already_in_progress(&self) -> bool {
matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::BuildAlreadyInProgress,
..
})
Expand All @@ -757,7 +767,7 @@ impl Error {
})
) || matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::ScopeNotFound,
..
})
Expand All @@ -776,7 +786,7 @@ impl Error {
})
) || matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::CollectionNotFound,
..
})
Expand All @@ -795,7 +805,7 @@ impl Error {
})
) || matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::IndexNotFound,
..
})
Expand All @@ -814,7 +824,7 @@ impl Error {
})
) || matches!(
self.kind(),
ErrorKind::ServerError(ServerError {
ErrorKind::Server(ServerError {
kind: ServerErrorKind::IndexExists,
..
})
Expand Down
Loading

0 comments on commit 2eddd57

Please sign in to comment.