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)]
#[non_exhaustive]
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>),
SchemaInferenceError(BigQuerySchemaInferenceError),
}
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),
BigQueryError::SchemaInferenceError(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()),
BigQueryError::SchemaInferenceError(ref err) => Some(err),
}
}
}
#[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 BigQuerySchemaInferenceError {
pub kind: BigQuerySchemaInferenceErrorKind,
pub path: Option<String>,
}
impl Display for BigQuerySchemaInferenceError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match &self.path {
None => write!(
f,
"Schema inference error: {}; declare the columns with `fields([..])`",
self.kind
),
Some(path) if self.kind == BigQuerySchemaInferenceErrorKind::UnknownColumn => {
write!(f, "Schema inference error on `{path}`: {}", self.kind)
}
Some(path) if self.kind == BigQuerySchemaInferenceErrorKind::UnresolvedAlias => write!(
f,
"Schema inference error on `{path}`: {}; declare the columns with `fields([..])`",
self.kind
),
Some(path) => write!(
f,
"Schema inference error on `{path}`: {}; declare its type with `.with(..)`",
self.kind
),
}
}
}
impl Error for BigQuerySchemaInferenceError {}
#[non_exhaustive]
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum BigQuerySchemaInferenceErrorKind {
NotAStruct,
UnknownKeys,
DynamicValue,
EnumWithData,
DateOrDateTime,
NestedArray,
NullableArrayElement,
NoColumnType,
RangeElement,
Recursive,
UnresolvedAlias,
Inconsistent,
UnknownColumn,
}
impl Display for BigQuerySchemaInferenceErrorKind {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
f.write_str(match self {
BigQuerySchemaInferenceErrorKind::NotAStruct => {
"the row type is not a struct with named fields"
}
BigQuerySchemaInferenceErrorKind::UnknownKeys => {
"a map or a `#[serde(flatten)]` struct does not list its keys"
}
BigQuerySchemaInferenceErrorKind::DynamicValue => {
"a self-describing value, such as `serde_json::Value` or an untagged enum, has \
no fixed type"
}
BigQuerySchemaInferenceErrorKind::EnumWithData => {
"an enum with a variant that carries data has no column type"
}
BigQuerySchemaInferenceErrorKind::DateOrDateTime => {
"the field reads both DATE and DATETIME text, as `jiff::civil::Date` and \
`jiff::civil::DateTime` both do"
}
BigQuerySchemaInferenceErrorKind::NestedArray => "BigQuery has no array of arrays",
BigQuerySchemaInferenceErrorKind::NullableArrayElement => {
"a BigQuery array cannot hold NULL elements"
}
BigQuerySchemaInferenceErrorKind::NoColumnType => {
"`()`, a unit or tuple struct, a tuple, an array of anything but `u8` or an enum \
with no variants has no column type"
}
BigQuerySchemaInferenceErrorKind::RangeElement => {
"a RANGE element must be DATE, DATETIME or TIMESTAMP"
}
BigQuerySchemaInferenceErrorKind::Recursive => "the type contains itself",
BigQuerySchemaInferenceErrorKind::UnresolvedAlias => {
"inference cannot tell whether this name is a field's own name or a \
`#[serde(alias)]`"
}
BigQuerySchemaInferenceErrorKind::Inconsistent => {
"the type's `Deserialize` asked for something else when it was run again"
}
BigQuerySchemaInferenceErrorKind::UnknownColumn => {
"`.with(..)` names no inferred column"
}
})
}
}
#[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),
Code::Cancelled if transport_cancelled(&status) => {
BigQueryError::database_with_code("CONNECTION_CLOSED", format!("{status}"), true)
}
_ => 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 transport_cancelled(status: &gcloud_sdk::tonic::Status) -> bool {
hyper_error(status).is_some_and(hyper::Error::is_canceled)
}
fn hyper_error(status: &gcloud_sdk::tonic::Status) -> Option<&hyper::Error> {
std::iter::successors(status.source(), |&source| source.source())
.find_map(|source| source.downcast_ref::<hyper::Error>())
}
fn check_hyper_errors(status: gcloud_sdk::tonic::Status) -> BigQueryError {
let lost = |code: &str, err: &hyper::Error| {
BigQueryError::database_with_code(code, format!("Hyper error: {err}"), true)
};
match hyper_error(&status) {
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;
use std::future::Future;
use std::pin::Pin;
use std::task::{ready, Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite};
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}");
}
struct Pipe(tokio::io::DuplexStream);
impl hyper::rt::Read for Pipe {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
mut buf: hyper::rt::ReadBufCursor<'_>,
) -> Poll<std::io::Result<()>> {
let mut bytes = [0; 1024];
let mut read = tokio::io::ReadBuf::new(&mut bytes);
ready!(Pin::new(&mut self.0).poll_read(cx, &mut read))?;
buf.put_slice(read.filled());
Poll::Ready(Ok(()))
}
}
impl hyper::rt::Write for Pipe {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.0).poll_write(cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_flush(cx)
}
fn poll_shutdown(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_shutdown(cx)
}
}
#[derive(Clone)]
struct TokioExecutor;
impl<F> hyper::rt::Executor<F> for TokioExecutor
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
fn execute(&self, future: F) {
tokio::spawn(future);
}
}
async fn call_on_a_closed_connection() -> Status {
let (client, _server) = tokio::io::duplex(64 * 1024);
let (mut sender, connection) =
hyper::client::conn::http2::handshake(TokioExecutor, Pipe(client))
.await
.expect("the handshake only writes the preface");
drop(connection);
let request = hyper::Request::new(String::new());
let error = sender
.send_request(request)
.await
.expect_err("the connection is gone");
Status::from_error(Box::new(error))
}
#[tokio::test]
async fn call_cancelled_by_a_closed_connection_is_retryable() {
let status = call_on_a_closed_connection().await;
assert_eq!(status.code(), Code::Cancelled);
let err = BigQueryError::from(status);
assert!(err.retry_possible(), "{err}");
}
#[derive(Debug)]
struct TransportError(hyper::Error);
impl Display for TransportError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("transport error")
}
}
impl Error for TransportError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.0)
}
}
fn under_transport_error(error: hyper::Error) -> Status {
let status = Status::from_error(Box::new(TransportError(error)));
assert_eq!(status.code(), Code::Unknown);
status
}
#[tokio::test]
async fn a_closed_connection_under_the_transport_error_is_connection_closed() {
let (client, _server) = tokio::io::duplex(64 * 1024);
let (mut sender, connection) =
hyper::client::conn::http2::handshake::<_, _, String>(TokioExecutor, Pipe(client))
.await
.expect("the handshake only writes the preface");
drop(connection);
let closed = sender.ready().await.expect_err("the connection is gone");
match BigQueryError::from(under_transport_error(closed)) {
BigQueryError::DatabaseError(err) => {
assert_eq!(err.public.code, "CONNECTION_CLOSED");
assert!(err.retry_possible);
}
other => panic!("expected a database error, got {other:?}"),
}
}
#[tokio::test]
async fn a_request_hyper_refused_under_the_transport_error_is_not_retryable() {
let (client, server) = tokio::io::duplex(64 * 1024);
tokio::spawn(async move {
if let Ok(mut connection) = h2::server::handshake(server).await {
while connection.accept().await.is_some() {}
}
});
let (mut sender, connection) =
hyper::client::conn::http2::handshake(TokioExecutor, Pipe(client))
.await
.expect("the handshake only writes the preface");
tokio::spawn(connection);
let request = hyper::Request::builder()
.method(hyper::Method::CONNECT)
.uri("http://example.com")
.body(String::from("a CONNECT over HTTP/2 takes no body"))
.expect("a valid request");
let refused = sender
.send_request(request)
.await
.expect_err("hyper refuses a CONNECT with a body");
assert!(refused.is_user(), "{refused:?}");
let err = BigQueryError::from(under_transport_error(refused));
assert!(!err.retry_possible(), "{err}");
}
#[test]
fn cancelled_by_the_server_is_not_retryable() {
let err = classify(Code::Cancelled, "operation was canceled");
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}");
}
}