xref: /aosp_15_r20/system/core/init/libprefetch/prefetch/src/error.rs (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1 // Copyright (C) 2024 The Android Open Source Project
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 //      http://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 use thiserror::Error;
16 
17 use crate::{format::FileId, InodeInfo};
18 
19 /// Enumerates all possible errors returned by this library.
20 #[derive(Debug, Error)]
21 pub enum Error {
22     /// Represents a failure to open a file.
23     #[error("Open error: {path}: {source}")]
24     Open {
25         /// The IO error
26         source: std::io::Error,
27         /// Path on which the operation failed.
28         path: String,
29     },
30 
31     /// Represents a failure to create a file.
32     #[error("Create error. {path} {source}")]
33     Create {
34         /// The IO error
35         source: std::io::Error,
36         /// Path on which the operation failed.
37         path: String,
38     },
39 
40     /// Represents a failure to read trace file.
41     #[error("Read error. {error}")]
42     Read {
43         /// Detailed error message.
44         error: String,
45     },
46 
47     /// Represents a failure to write to a file.
48     #[error("Write error. {source}")]
49     Write {
50         /// The IO error
51         source: std::io::Error,
52 
53         /// file path
54         path: String,
55     },
56 
57     /// Represents a failure to delete a file.
58     #[error("Delete error. {path} {source}")]
59     Delete {
60         /// The IO error
61         source: std::io::Error,
62         /// Path on which the operation failed.
63         path: String,
64     },
65 
66     /// Represents a failure to stat a file.
67     #[error("Stat error. {path} {source}")]
68     Stat {
69         /// The IO error
70         source: std::io::Error,
71         /// Path on which the operation failed.
72         path: String,
73     },
74 
75     /// Represents a failure to stat a file.
76     #[error("clone failed. {id} {source}")]
77     FileClone {
78         /// The IO error
79         source: std::io::Error,
80         /// File id for which we could not clone the file.
81         id: FileId,
82     },
83 
84     /// Represents a failure to mmap a file.
85     #[error("mmap failed. {path} {error}")]
86     Mmap {
87         /// Detailed error message.
88         error: String,
89         /// Path on which the operation failed.
90         path: String,
91     },
92 
93     /// Represents a failure to munmap a file.
94     #[error("munmap failed. {length} {error}")]
95     Munmap {
96         /// Detailed error message.
97         error: String,
98         /// Size of file which this munmap failed
99         length: usize,
100     },
101 
102     /// Represents all other cases of `std::io::Error`.
103     ///
104     #[error(transparent)]
105     IoError(
106         /// The IO error
107         #[from]
108         std::io::Error,
109     ),
110 
111     /// Represents a failure to map FileId to path
112     ///
113     #[error("Failed to map id to path: {id}")]
114     IdNoFound {
115         /// File id for which path lookup failed.
116         id: FileId,
117     },
118 
119     /// Indicates that the file is skipped for prefetching
120     /// because it is in the exclude files list.
121     ///
122     #[error("Skipped prefetching file from path: {path}")]
123     SkipPrefetch {
124         /// Path to file for which prefetching is skipped.
125         path: String,
126     },
127 
128     /// Represents spurious InodeInfo or missing Record.
129     ///
130     #[error(
131         "Stale inode(s) info found.\n\
132             missing_file_ids: {missing_file_ids:#?}\n\
133             stale_inodes: {stale_inodes:#?} \n\
134             missing_paths:{missing_paths:#?}"
135     )]
136     StaleInode {
137         /// FileIds for which InodeInfo is missing.
138         missing_file_ids: Vec<FileId>,
139 
140         /// InodeInfos for which no records exist.
141         stale_inodes: Vec<InodeInfo>,
142 
143         /// InodeInfos in which no paths were found.
144         missing_paths: Vec<InodeInfo>,
145     },
146 
147     /// Represents a failure to serialize records file.
148     #[error("Serialize error: {error}")]
149     Serialize {
150         /// Detailed error message.
151         error: String,
152     },
153 
154     /// Represents a failure to deserialize records file.
155     #[error("Deserialize error: {error}")]
156     Deserialize {
157         /// Detailed error message.
158         error: String,
159     },
160 
161     /// Represents a failure from thread pool.
162     #[error("Thread pool error: {error}")]
163     ThreadPool {
164         /// Detailed error message.
165         error: String,
166     },
167 
168     /// Represents a failure to setup file.
169     #[error("Failed to setup prefetch: {error}")]
170     Custom {
171         /// Detailed error message.
172         error: String,
173     },
174 
175     /// Represents a failure to parse args.
176     #[error("Failed to parse arg:{arg_name} value:{arg_value} error:{error}")]
177     InvalidArgs {
178         /// Arg name.
179         arg_name: String,
180 
181         /// Arg value.
182         arg_value: String,
183 
184         /// Detailed error message.
185         error: String,
186     },
187 }
188