Skip to main content

google_cloud_bigquery/write/
error.rs

1// Copyright 2026 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::Error;
16use crate::model::RowError;
17
18/// Represents an error that can occur when appending rows.
19#[derive(thiserror::Error, Debug)]
20#[non_exhaustive]
21pub enum AppendError {
22    /// The underlying RPC failed.
23    #[non_exhaustive]
24    #[error("the operation failed. RPC error: {source}")]
25    Rpc {
26        /// The error returned by the service for the request.
27        #[from]
28        #[source]
29        source: Error,
30    },
31
32    /// Certain rows have errors.
33    #[error(
34        "the service reports an error for the following rows. No rows in the batch were appended. You can remove the bad rows and retry the request. Rows: {0:?}"
35    )]
36    RowErrors(Vec<RowError>),
37
38    /// The `AppendRows` stream closed unexpectedly.
39    #[error(
40        "the `AppendRows` stream closed unexpectedly and the client library could not recover."
41    )]
42    UnexpectedEndOfStream,
43}
44
45pub(crate) type AppendResult<T> = std::result::Result<T, AppendError>;
46
47/// Represents an error that can occur when attaching to an existing stream.
48#[derive(thiserror::Error, Debug)]
49#[non_exhaustive]
50pub enum AttachError {
51    /// The stream type provided by the service did not match the expected type.
52    #[error("stream type mismatch: requested {expected:?}, but matched resource yields {actual:?}")]
53    TypeMismatch {
54        /// The expected stream type.
55        expected: crate::model::write_stream::Type,
56        /// The actual stream type returned by the service.
57        actual: crate::model::write_stream::Type,
58    },
59
60    /// The underlying RPC failed.
61    #[non_exhaustive]
62    #[error("the operation failed. RPC error: {source}")]
63    Rpc {
64        /// The error returned by the service for the request.
65        #[from]
66        #[source]
67        source: Error,
68    },
69}
70
71pub(crate) type AttachResult<T> = std::result::Result<T, AttachError>;
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76    use google_cloud_gax::error::rpc::{Code, Status};
77
78    #[test]
79    fn append_error_rpc_debug() {
80        let e = AppendError::Rpc {
81            source: Error::service(
82                Status::default()
83                    .set_code(Code::FailedPrecondition)
84                    .set_message("inner fail"),
85            ),
86        };
87        let fmt = format!("{e}");
88        assert!(fmt.contains("operation failed."), "{fmt}");
89        assert!(fmt.contains("inner fail"), "{fmt}");
90    }
91}