google_cloud_storage/
error.rs

1// Copyright 2025 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
15//! Custom errors for the Cloud Storage client.
16//!
17//! The storage client defines additional error types. These are often returned
18//! as the `source()` of an [Error][crate::Error].
19
20use crate::model::{Object, ObjectChecksums};
21
22/// Indicates that a checksum mismatch was detected while reading or writing
23/// Cloud Storage object.
24///
25/// When performing a full read of an object, the client library automatically
26/// computes the CRC32C checksum (and optionally the MD5 hash) of the received
27/// data. At the end of the read The client library automatically computes this
28/// checksum to the values reported by the service. If the values do not match,
29/// the read operation completes with an error and the error includes this type
30/// showing which checksums did not match.
31///
32/// Likewise, when performing an object write, the client library automatically
33/// compares the CRC32C checksum (and optionally the MD5 hash) of the data sent
34/// to the service against the values reported by the service when the object is
35/// finalized. If the values do not match, the write operation completes with an
36/// error and the error includes this type.
37#[derive(Clone, Debug)]
38#[non_exhaustive]
39pub enum ChecksumMismatch {
40    /// The CRC32C checksum sent by the service does not match the computed (or expected) value.
41    Crc32c { got: u32, want: u32 },
42
43    /// The MD5 hash sent by the service does not match the computed (or expected) value.
44    Md5 {
45        got: bytes::Bytes,
46        want: bytes::Bytes,
47    },
48
49    /// The CRC32C checksum **and** the MD5 hash sent by the service do not
50    /// match the computed (or expected) values.
51    Both {
52        got: Box<ObjectChecksums>,
53        want: Box<ObjectChecksums>,
54    },
55}
56
57impl std::fmt::Display for ChecksumMismatch {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self {
60            Self::Crc32c { got, want } => write!(
61                f,
62                "the CRC32C checksums do not match: got=0x{got:08x}, want=0x{want:08x}"
63            ),
64            Self::Md5 { got, want } => write!(
65                f,
66                "the MD5 hashes do not match: got={:0x?}, want={:0x?}",
67                &got, &want
68            ),
69            Self::Both { got, want } => {
70                write!(
71                    f,
72                    "both the CRC32C checksums and MD5 hashes do not match: got.crc32c=0x{:08x}, want.crc32c=0x{:08x}, got.md5={:x?}, want.md5={:x?}",
73                    got.crc32c.unwrap_or_default(),
74                    want.crc32c.unwrap_or_default(),
75                    got.md5_hash,
76                    want.md5_hash
77                )
78            }
79        }
80    }
81}
82
83/// Represents errors that can occur when converting to [KeyAes256] instances.
84///
85/// # Example:
86/// ```
87/// # use google_cloud_storage::{model_ext::KeyAes256, error::KeyAes256Error};
88/// let invalid_key_bytes: &[u8] = b"too_short_key"; // Less than 32 bytes
89/// let result = KeyAes256::new(invalid_key_bytes);
90///
91/// assert!(matches!(result, Err(KeyAes256Error::InvalidLength)));
92/// ```
93///
94/// [KeyAes256]: crate::model_ext::KeyAes256
95#[derive(thiserror::Error, Debug)]
96#[non_exhaustive]
97pub enum KeyAes256Error {
98    /// The provided key's length was not exactly 32 bytes.
99    #[error("Key has an invalid length: expected 32 bytes.")]
100    InvalidLength,
101}
102
103/// Represents an error that can occur when reading response data.
104#[derive(thiserror::Error, Debug)]
105#[non_exhaustive]
106pub enum ReadError {
107    /// The calculated crc32c did not match server provided crc32c.
108    #[error("checksum mismatch {0}")]
109    ChecksumMismatch(ChecksumMismatch),
110
111    /// The read was interrupted before all the expected bytes arrived.
112    #[error("missing {0} bytes at the end of the stream")]
113    ShortRead(u64),
114
115    /// The read received more bytes than expected.
116    #[error("too many bytes received: expected {expected}, stopped read at {got}")]
117    LongRead { got: u64, expected: u64 },
118
119    /// Only 200 and 206 status codes are expected in successful responses.
120    #[error("unexpected success code {0} in read request, only 200 and 206 are expected")]
121    UnexpectedSuccessCode(u16),
122
123    /// Successful HTTP response must include some headers.
124    #[error("the response is missing '{0}', a required header")]
125    MissingHeader(&'static str),
126
127    /// The received header format is invalid.
128    #[error("the format for header '{0}' is incorrect")]
129    BadHeaderFormat(
130        &'static str,
131        #[source] Box<dyn std::error::Error + Send + Sync + 'static>,
132    ),
133}
134
135/// An unrecoverable problem in the upload protocol.
136///
137/// # Example
138/// ```
139/// # use google_cloud_storage::{client::Storage, error::WriteError};
140/// # async fn sample(client: &Storage) -> anyhow::Result<()> {
141/// use std::error::Error as _;
142/// let writer = client
143///     .write_object("projects/_/buckets/my-bucket", "my-object", "hello world")
144///     .set_if_generation_not_match(0);
145/// match writer.send_buffered().await {
146///     Ok(object) => println!("Successfully created the object {object:?}"),
147///     Err(error) if error.is_serialization() => {
148///         println!("Some problem {error:?} sending the data to the service");
149///         if let Some(m) = error.source().and_then(|e| e.downcast_ref::<WriteError>()) {
150///             println!("{m}");
151///         }
152///     },
153///     Err(e) => return Err(e.into()), // not handled in this example
154/// }
155/// # Ok(()) }
156/// ```
157///
158#[derive(thiserror::Error, Debug)]
159#[non_exhaustive]
160pub enum WriteError {
161    /// The service has "uncommitted" previously persisted bytes.
162    ///
163    /// # Troubleshoot
164    ///
165    /// In the resumable upload protocol the service reports how many bytes are
166    /// persisted. This error indicates that the service previously reported
167    /// more bytes as persisted than in the latest report. This could indicate:
168    /// - a corrupted message from the service, either the earlier message
169    ///   reporting more bytes persisted than actually are, or the current
170    ///   message indicating fewer bytes persisted.
171    /// - a bug in the service, where it reported bytes as persisted when they
172    ///   were not.
173    /// - a bug in the client, maybe storing the incorrect byte count, or
174    ///   parsing the messages incorrectly.
175    ///
176    /// All of these conditions indicate a bug, and in Rust it is idiomatic to
177    /// `panic!()` when a bug is detected. However, in this case it seems more
178    /// appropriate to report the problem, as the client library cannot
179    /// determine the location of the bug.
180    #[error(
181        "the service previously persisted {offset} bytes, but now reports only {persisted} as persisted"
182    )]
183    UnexpectedRewind { offset: u64, persisted: u64 },
184
185    /// The service reports more bytes persisted than sent.
186    ///
187    /// # Troubleshoot
188    ///
189    /// Most likely this indicates that two concurrent uploads are using the
190    /// same session. Review your application design to avoid concurrent
191    /// uploads.
192    ///
193    /// It is possible that this indicates a bug in the service, client, or
194    /// messages corrupted in transit.
195    #[error("the service reports {persisted} bytes as persisted, but we only sent {sent} bytes")]
196    TooMuchProgress { sent: u64, persisted: u64 },
197
198    /// The checksums reported by the service do not match the expected checksums.
199    ///
200    /// # Troubleshoot
201    ///
202    /// The client library compares the CRC32C checksum and/or MD5 hash of the
203    /// uploaded data against the hash reported by the service at the end of
204    /// the upload. This error indicates the hashes did not match.
205    ///
206    /// If you provided known values for these checksums verify those values are
207    /// correct.
208    ///
209    /// Otherwise, this is probably a data corruption problem. These are
210    /// notoriously difficult to root cause. They probably indicate faulty
211    /// equipment, such as the physical machine hosting your client, the network
212    /// elements between your client and the service, or the physical machine
213    /// hosting the service.
214    ///
215    /// If possible, resend the data from a different machine.
216    #[error("checksum mismatch {mismatch} when uploading {} to {}", object.name, object.bucket)]
217    ChecksumMismatch {
218        mismatch: ChecksumMismatch,
219        object: Box<Object>,
220    },
221}
222
223#[cfg(test)]
224mod tests {
225    use super::*;
226
227    #[test]
228    fn mismatch_crc32c() {
229        let value = ChecksumMismatch::Crc32c {
230            got: 0x01020304_u32,
231            want: 0x02030405_u32,
232        };
233        let fmt = value.to_string();
234        assert!(fmt.contains("got=0x01020304"), "{value:?} => {fmt}");
235        assert!(fmt.contains("want=0x02030405"), "{value:?} => {fmt}");
236    }
237
238    #[test]
239    fn mismatch_md5() {
240        let value = ChecksumMismatch::Md5 {
241            got: bytes::Bytes::from_owner([0x01_u8, 0x02, 0x03, 0x04]),
242            want: bytes::Bytes::from_owner([0x02_u8, 0x03, 0x04, 0x05]),
243        };
244        let fmt = value.to_string();
245        assert!(
246            fmt.contains(r#"got=b"\x01\x02\x03\x04""#),
247            "{value:?} => {fmt}"
248        );
249        assert!(
250            fmt.contains(r#"want=b"\x02\x03\x04\x05""#),
251            "{value:?} => {fmt}"
252        );
253    }
254
255    #[test]
256    fn mismatch_both() {
257        let got = ObjectChecksums::new()
258            .set_crc32c(0x01020304_u32)
259            .set_md5_hash(bytes::Bytes::from_owner([0x01_u8, 0x02, 0x03, 0x04]));
260        let want = ObjectChecksums::new()
261            .set_crc32c(0x02030405_u32)
262            .set_md5_hash(bytes::Bytes::from_owner([0x02_u8, 0x03, 0x04, 0x05]));
263        let value = ChecksumMismatch::Both {
264            got: Box::new(got),
265            want: Box::new(want),
266        };
267        let fmt = value.to_string();
268        assert!(fmt.contains("got.crc32c=0x01020304"), "{value:?} => {fmt}");
269        assert!(fmt.contains("want.crc32c=0x02030405"), "{value:?} => {fmt}");
270        assert!(
271            fmt.contains(r#"got.md5=b"\x01\x02\x03\x04""#),
272            "{value:?} => {fmt}"
273        );
274        assert!(
275            fmt.contains(r#"want.md5=b"\x02\x03\x04\x05""#),
276            "{value:?} => {fmt}"
277        );
278    }
279}