asset_io/error.rs
1// Copyright 2026 Adobe. All rights reserved.
2// This file is licensed to you under the Apache License,
3// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
4// or the MIT license (http://opensource.org/licenses/MIT),
5// at your option.
6
7// Unless required by applicable law or agreed to in writing,
8// this software is distributed on an "AS IS" BASIS, WITHOUT
9// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
10// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
11// specific language governing permissions and limitations under
12// each license.
13
14//! Error types
15
16use std::io;
17
18/// Result type for container operations
19pub type Result<T> = std::result::Result<T, Error>;
20
21/// Errors that can occur while reading or writing a container
22#[derive(Debug, thiserror::Error)]
23#[non_exhaustive]
24pub enum Error {
25 /// I/O error
26 #[error("I/O error: {0}")]
27 Io(#[from] io::Error),
28
29 /// A caller-supplied callback (for example a [`Sink`](crate::Sink))
30 /// stopped the operation, such as on user cancel.
31 #[error("operation canceled by a callback")]
32 UserCanceled,
33
34 /// A caller-supplied callback (for example a [`Sink`](crate::Sink)) failed
35 /// with its own error, returned unchanged so the caller can recover it by
36 /// downcasting.
37 #[error(transparent)]
38 Callback(Box<dyn std::error::Error + Send + Sync>),
39
40 /// Invalid file format
41 #[error("Invalid container: {0}")]
42 InvalidFormat(String),
43
44 /// Unsupported file format
45 #[error("Unsupported format")]
46 UnsupportedFormat,
47
48 /// A recognized format uses a feature or operation this reader cannot support.
49 #[error("Unsupported feature: {0}")]
50 UnsupportedFeature(&'static str),
51
52 /// The container cannot hold, or this handler cannot write, an item of
53 /// this kind. A write plan reports it for an
54 /// [`Updates`](crate::Updates) value it cannot place, before anything is
55 /// written.
56 #[error("cannot write {0} to this container")]
57 UnsupportedItem(crate::ItemKind),
58
59 /// Invalid segment
60 #[error("Invalid segment at offset {offset}: {reason}")]
61 InvalidSegment { offset: u64, reason: String },
62
63 /// A required item is absent.
64 #[error("{what} not found")]
65 NotFound { what: &'static str },
66
67 /// More than one candidate where exactly one is required.
68 #[error("{what} is ambiguous ({count} candidates)")]
69 Ambiguous { what: &'static str, count: usize },
70
71 /// A resource limit was exceeded.
72 #[error("limit exceeded: {what} (limit {limit})")]
73 LimitExceeded { what: &'static str, limit: u64 },
74
75 /// XML parsing error (from quick-xml)
76 #[cfg(feature = "xmp")]
77 #[error("XML error: {0}")]
78 Xml(#[from] quick_xml::Error),
79}
80
81impl Error {
82 /// [`Error::Callback`] holding `error`, for a callback to return its own error
83 /// through asset-io.
84 pub fn callback(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self {
85 Error::Callback(error.into())
86 }
87
88 /// [`Error::InvalidSegment`]: the data at `offset` is not a valid element of the
89 /// container, for the given `reason`.
90 pub fn invalid_segment(offset: u64, reason: impl Into<String>) -> Self {
91 Error::InvalidSegment {
92 offset,
93 reason: reason.into(),
94 }
95 }
96}