use crate::{BigQueryJobRef, BigQueryTablePlan, BigQueryTableRef};
use gcloud_sdk::tonic::Code;
use rsb_derive::Builder;
use std::error::Error;
use std::fmt::Display;
use std::fmt::Formatter;
#[derive(Debug, Clone)]
pub enum BigQueryError {
SystemError(BigQuerySystemError),
DatabaseError(BigQueryDatabaseError),
DataConflictError(BigQueryDataConflictError),
DataNotFoundError(BigQueryDataNotFoundError),
InvalidParametersError(BigQueryInvalidParametersError),
SerializeError(BigQuerySerializationError),
DeserializeError(BigQuerySerializationError),
RowErrors(BigQueryRowErrors),
SchemaMismatchError(BigQuerySchemaMismatchError),
WriteStreamError(BigQueryWriteStreamError),
JobError(Box<BigQueryJobError>),
SchemaChangeRefused(Box<BigQuerySchemaChangeRefusedError>),
}
impl BigQueryError {
pub(crate) fn invalid_parameters(field: impl Into<String>, error: impl Into<String>) -> Self {
BigQueryError::InvalidParametersError(BigQueryInvalidParametersError::new(
BigQueryInvalidParametersPublicDetails::new(field.into(), error.into()),
))
}
pub(crate) fn unsupported_table(
table: &gcloud_sdk::google::cloud::bigquery::v2::Table,
what: String,
) -> Self {
let id = table
.table_reference
.as_ref()
.map(|reference| {
format!(
"{}.{}.{}",
reference.project_id, reference.dataset_id, reference.table_id
)
})
.unwrap_or_default();
BigQueryError::invalid_parameters("table", format!("{id}: {what}"))
}
pub(crate) fn system(code: &str, message: impl Into<String>) -> Self {
BigQueryError::SystemError(BigQuerySystemError::new(
BigQueryErrorPublicGenericDetails::new(code.to_string()),
message.into(),
))
}
pub(crate) fn unexpected_response(message: impl Into<String>) -> Self {
Self::system("UNEXPECTED_RESPONSE", message)
}
pub(crate) fn database(code: Code, details: impl Into<String>, retry_possible: bool) -> Self {
Self::database_with_code(&code_name(code), details, retry_possible)
}
fn database_with_code(code: &str, details: impl Into<String>, retry_possible: bool) -> Self {
BigQueryError::DatabaseError(BigQueryDatabaseError::new(
BigQueryErrorPublicGenericDetails::new(code.to_string()),
details.into(),
retry_possible,
))
}
pub(crate) fn schema_mismatch(
code: &str,
table: BigQueryTableRef,
details: impl Into<String>,
) -> Self {
BigQueryError::SchemaMismatchError(BigQuerySchemaMismatchError::new(
BigQueryErrorPublicGenericDetails::new(code.to_string()),
table,
details.into(),
))
}
pub(crate) fn write_stream(
code: &str,
stream: impl Into<String>,
details: impl Into<String>,
) -> Self {
BigQueryError::WriteStreamError(BigQueryWriteStreamError::new(
BigQueryErrorPublicGenericDetails::new(code.to_string()),
stream.into(),
details.into(),
))
}
pub(crate) fn has_code(&self, code: Code) -> bool {
matches!(self, BigQueryError::DatabaseError(err) if err.public.code == code_name(code))
}
pub(crate) fn on_stale_etag(self, stale: impl FnOnce() -> String) -> Self {
match self {
BigQueryError::DatabaseError(err)
if err.public.code == code_name(Code::FailedPrecondition) =>
{
BigQueryError::DataConflictError(BigQueryDataConflictError::new(
err.public,
format!("{} {}", stale(), err.details),
))
}
other => other,
}
}
pub fn retry_possible(&self) -> bool {
matches!(self, BigQueryError::DatabaseError(err) if err.retry_possible)
}
}
pub(crate) fn code_name(code: Code) -> String {
format!("{code:?}")
}
impl Display for BigQueryError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match *self {
BigQueryError::SystemError(ref err) => err.fmt(f),
BigQueryError::DatabaseError(ref err) => err.fmt(f),
BigQueryError::DataConflictError(ref err) => err.fmt(f),
BigQueryError::DataNotFoundError(ref err) => err.fmt(f),
BigQueryError::InvalidParametersError(ref err) => err.fmt(f),
BigQueryError::SerializeError(ref err) => write!(f, "Serialize error: {err}"),
BigQueryError::DeserializeError(ref err) => write!(f, "Deserialize error: {err}"),
BigQueryError::RowErrors(ref err) => err.fmt(f),
BigQueryError::SchemaMismatchError(ref err) => err.fmt(f),
BigQueryError::WriteStreamError(ref err) => err.fmt(f),
BigQueryError::JobError(ref err) => err.fmt(f),
BigQueryError::SchemaChangeRefused(ref err) => err.fmt(f),
}
}
}
impl Error for BigQueryError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
BigQueryError::SystemError(ref err) => Some(err),
BigQueryError::DatabaseError(ref err) => Some(err),
BigQueryError::DataConflictError(ref err) => Some(err),
BigQueryError::DataNotFoundError(ref err) => Some(err),
BigQueryError::InvalidParametersError(ref err) => Some(err),
BigQueryError::SerializeError(ref err) => Some(err),
BigQueryError::DeserializeError(ref err) => Some(err),
BigQueryError::RowErrors(ref err) => Some(err),
BigQueryError::SchemaMismatchError(ref err) => Some(err),
BigQueryError::WriteStreamError(ref err) => Some(err),
BigQueryError::JobError(ref err) => Some(err.as_ref()),
BigQueryError::SchemaChangeRefused(ref err) => Some(err.as_ref()),
}
}
}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryErrorPublicGenericDetails {
pub code: String,
}
impl Display for BigQueryErrorPublicGenericDetails {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Error code: {}", self.code)
}
}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQuerySystemError {
pub public: BigQueryErrorPublicGenericDetails,
pub message: String,
}
impl Display for BigQuerySystemError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"BigQuery system/internal error: {}. {}",
self.public, self.message
)
}
}
impl Error for BigQuerySystemError {}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryDatabaseError {
pub public: BigQueryErrorPublicGenericDetails,
pub details: String,
pub retry_possible: bool,
}
impl Display for BigQueryDatabaseError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"Database general error occurred: {}. {}. Retry possibility: {}",
self.public, self.details, self.retry_possible
)
}
}
impl Error for BigQueryDatabaseError {}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryDataConflictError {
pub public: BigQueryErrorPublicGenericDetails,
pub details: String,
}
impl Display for BigQueryDataConflictError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"Database conflict error occurred: {}. {}",
self.public, self.details
)
}
}
impl Error for BigQueryDataConflictError {}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryDataNotFoundError {
pub public: BigQueryErrorPublicGenericDetails,
pub data_detail_message: String,
}
impl Display for BigQueryDataNotFoundError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"Data not found error occurred: {}. {}",
self.public, self.data_detail_message
)
}
}
impl Error for BigQueryDataNotFoundError {}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryInvalidParametersPublicDetails {
pub field: String,
pub error: String,
}
impl Display for BigQueryInvalidParametersPublicDetails {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"Invalid parameters error: {}. {}",
self.field, self.error
)
}
}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryInvalidParametersError {
pub public: BigQueryInvalidParametersPublicDetails,
}
impl Display for BigQueryInvalidParametersError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{}", self.public)
}
}
impl Error for BigQueryInvalidParametersError {}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQuerySerializationError {
pub public: BigQueryErrorPublicGenericDetails,
pub kind: BigQueryCodecErrorKind,
pub row: Option<u64>,
pub path: String,
pub message: String,
}
impl Display for BigQuerySerializationError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{}", self.public)?;
if let Some(row) = self.row {
write!(f, ", row {row}")?;
}
if !self.path.is_empty() {
write!(f, ", field `{}`", self.path)?;
}
write!(f, ": {}", self.message)
}
}
impl Error for BigQuerySerializationError {}
#[non_exhaustive]
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum BigQueryCodecErrorKind {
TypeMismatch,
NullForNonOption,
NullForRequired,
NullArrayElement,
OutOfRange,
InvalidText,
UnknownField,
MissingRequiredField,
UnsupportedType,
RowTooLarge,
Custom,
}
impl BigQueryCodecErrorKind {
pub fn code(self) -> &'static str {
match self {
BigQueryCodecErrorKind::TypeMismatch => "TYPE_MISMATCH",
BigQueryCodecErrorKind::NullForNonOption => "NULL_FOR_NON_OPTION",
BigQueryCodecErrorKind::NullForRequired => "NULL_FOR_REQUIRED",
BigQueryCodecErrorKind::NullArrayElement => "NULL_ARRAY_ELEMENT",
BigQueryCodecErrorKind::OutOfRange => "OUT_OF_RANGE",
BigQueryCodecErrorKind::InvalidText => "INVALID_TEXT",
BigQueryCodecErrorKind::UnknownField => "UNKNOWN_FIELD",
BigQueryCodecErrorKind::MissingRequiredField => "MISSING_REQUIRED_FIELD",
BigQueryCodecErrorKind::UnsupportedType => "UNSUPPORTED_TYPE",
BigQueryCodecErrorKind::RowTooLarge => "ROW_TOO_LARGE",
BigQueryCodecErrorKind::Custom => "CUSTOM",
}
}
}
impl Display for BigQueryCodecErrorKind {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
f.write_str(self.code())
}
}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryRowErrors {
pub public: BigQueryErrorPublicGenericDetails,
pub batch_index: u64,
pub first_row: u64,
pub row_count: u64,
pub errors: Vec<BigQueryRowError>,
}
impl Display for BigQueryRowErrors {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"{}: batch {} (rows {} to {}) was rejected with {} row errors",
self.public,
self.batch_index,
self.first_row,
(self.first_row + self.row_count).saturating_sub(1),
self.errors.len()
)?;
if let Some(first) = self.errors.first() {
write!(f, ", first: row {}: {}", first.row, first.message)?;
}
Ok(())
}
}
impl Error for BigQueryRowErrors {}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryRowError {
pub row: u64,
pub code: String,
pub message: String,
}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQuerySchemaMismatchError {
pub public: BigQueryErrorPublicGenericDetails,
pub table: BigQueryTableRef,
pub details: String,
}
impl Display for BigQuerySchemaMismatchError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"Schema mismatch on {}: {}. {}",
self.table, self.public, self.details
)
}
}
impl Error for BigQuerySchemaMismatchError {}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryWriteStreamError {
pub public: BigQueryErrorPublicGenericDetails,
pub stream: String,
pub details: String,
}
impl Display for BigQueryWriteStreamError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"Write stream error on {}: {}. {}",
self.stream, self.public, self.details
)
}
}
impl Error for BigQueryWriteStreamError {}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryJobError {
pub public: BigQueryErrorPublicGenericDetails,
pub job: Option<BigQueryJobRef>,
pub details: String,
pub errors: Vec<BigQueryJobErrorEntry>,
}
impl Display for BigQueryJobError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Job error: {}", self.public)?;
if let Some(job) = &self.job {
write!(f, ", job {}:{}", job.project_id, job.job_id)?;
}
write!(f, ". {}", self.details)
}
}
impl Error for BigQueryJobError {}
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct BigQuerySchemaChangeRefusedError {
pub plan: BigQueryTablePlan,
}
impl Display for BigQuerySchemaChangeRefusedError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"Schema sync of {} refused, nothing was written: {} changes are impossible in place",
self.plan.table,
self.plan.impossible.len()
)?;
if let Some(refusal) = &self.plan.refusal {
write!(f, " and {refusal}")?;
}
Ok(())
}
}
impl Error for BigQuerySchemaChangeRefusedError {}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryJobErrorEntry {
pub reason: String,
pub location: String,
pub message: String,
}
impl From<gcloud_sdk::error::Error> for BigQueryError {
fn from(error: gcloud_sdk::error::Error) -> Self {
BigQueryError::SystemError(BigQuerySystemError::new(
BigQueryErrorPublicGenericDetails::new(format!("{:?}", error.kind())),
format!("GCloud system error: {error}"),
))
}
}
impl From<gcloud_sdk::tonic::Status> for BigQueryError {
fn from(status: gcloud_sdk::tonic::Status) -> Self {
let code = status.code();
match code {
Code::AlreadyExists => {
BigQueryError::DataConflictError(BigQueryDataConflictError::new(
BigQueryErrorPublicGenericDetails::new(code_name(code)),
format!("{status}"),
))
}
Code::NotFound => BigQueryError::DataNotFoundError(BigQueryDataNotFoundError::new(
BigQueryErrorPublicGenericDetails::new(code_name(code)),
format!("{status}"),
)),
Code::Aborted | Code::Unavailable | Code::ResourceExhausted | Code::Internal => {
Self::database(code, format!("{status}"), true)
}
Code::PermissionDenied | Code::InvalidArgument if is_rate_limit(&status) => {
Self::database(code, format!("{status}"), true)
}
Code::Unknown => check_hyper_errors(status),
_ => Self::database(code, format!("{status}"), false),
}
}
}
fn is_rate_limit(status: &gcloud_sdk::tonic::Status) -> bool {
status
.message()
.to_ascii_lowercase()
.contains("exceeded rate limits")
}
fn check_hyper_errors(status: gcloud_sdk::tonic::Status) -> BigQueryError {
let hyper_error = status
.source()
.and_then(|source| source.downcast_ref::<hyper::Error>());
let lost = |code: &str, err: &hyper::Error| {
BigQueryError::database_with_code(code, format!("Hyper error: {err}"), true)
};
match hyper_error {
Some(err) if err.is_closed() => lost("CONNECTION_CLOSED", err),
Some(err) if err.is_timeout() => lost("CONNECTION_TIMEOUT", err),
Some(err) if err.is_user() || err.is_parse() => {
BigQueryError::database(status.code(), format!("Hyper error: {err}"), false)
}
Some(err) => lost("CONNECTION_LOST", err),
None if status.message().contains("transport error") => {
BigQueryError::database_with_code("CONNECTION_ERROR", format!("{status}"), true)
}
None => BigQueryError::database(status.code(), format!("{status}"), false),
}
}
#[cfg(test)]
mod tests {
use super::*;
use gcloud_sdk::tonic::Status;
fn classify(code: Code, message: &str) -> BigQueryError {
BigQueryError::from(Status::new(code, message))
}
#[test]
fn transient_codes_are_retryable() {
for code in [
Code::Unavailable,
Code::ResourceExhausted,
Code::Aborted,
Code::Internal,
] {
let err = classify(code, "backend error");
assert!(err.retry_possible(), "{code:?} must be retryable: {err}");
}
}
#[test]
fn permanent_codes_are_not_retryable() {
for code in [
Code::InvalidArgument,
Code::PermissionDenied,
Code::Unauthenticated,
Code::FailedPrecondition,
Code::OutOfRange,
Code::Unimplemented,
] {
let err = classify(
code,
"Syntax error: Unexpected identifier \"SELEC\" at [1:1]",
);
assert!(
!err.retry_possible(),
"{code:?} must not be retryable: {err}"
);
assert!(matches!(err, BigQueryError::DatabaseError(_)), "{err:?}");
}
}
#[test]
fn patch_rate_limit_under_permission_denied_is_retryable() {
let err = classify(
Code::PermissionDenied,
"Exceeded rate limits: too many table update operations for this table. For more \
information, see https://cloud.google.com/bigquery/docs/troubleshoot-quotas",
);
assert!(err.retry_possible(), "{err}");
}
#[test]
fn ddl_rate_limit_under_invalid_argument_is_retryable() {
let err = classify(
Code::InvalidArgument,
"Job exceeded rate limits: Your table exceeded quota for table update operations. \
For more information, see https://cloud.google.com/bigquery/docs/troubleshoot-quotas",
);
assert!(err.retry_possible(), "{err}");
}
#[test]
fn rate_limit_error_keeps_the_reported_code() {
match classify(Code::PermissionDenied, "Exceeded rate limits: too many") {
BigQueryError::DatabaseError(err) => assert_eq!(err.public.code, "PermissionDenied"),
other => panic!("expected a database error, got {other:?}"),
}
}
#[test]
fn not_found_is_data_not_found() {
let err = classify(
Code::NotFound,
"Not found: Dataset latestbit:ds was not found in location US",
);
match err {
BigQueryError::DataNotFoundError(err) => {
assert!(err.data_detail_message.contains("latestbit:ds"), "{err}");
}
other => panic!("expected DataNotFoundError, got {other:?}"),
}
}
#[test]
fn already_exists_is_data_conflict() {
let err = classify(Code::AlreadyExists, "Already Exists: Dataset latestbit:ds");
assert!(
matches!(err, BigQueryError::DataConflictError(_)),
"{err:?}"
);
assert!(!err.retry_possible());
}
#[test]
fn unknown_transport_error_is_retryable() {
let err = classify(Code::Unknown, "transport error");
assert!(err.retry_possible(), "{err}");
}
#[test]
fn unknown_without_cause_is_not_retryable() {
let err = classify(Code::Unknown, "something odd");
assert!(!err.retry_possible(), "{err}");
}
}