1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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 
16 use std::fmt::{Debug, Display, Formatter};
17 
18 use ipc::parcel::{Deserialize, MsgParcel};
19 use ipc::{IpcResult, IpcStatusCode};
20 
21 use crate::ipc_conn::vec_raw_read;
22 
23 #[derive(Default, Debug)]
24 pub(crate) struct Errors(pub(crate) Vec<Error>);
25 
26 impl Deserialize for Errors {
deserialize(parcel: &mut MsgParcel) -> IpcResult<Self>27     fn deserialize(parcel: &mut MsgParcel) -> IpcResult<Self> {
28         let result = Errors(vec_raw_read::<Error>(parcel)?);
29         Ok(result)
30     }
31 }
32 
33 #[derive(Debug, Eq, PartialEq, Clone)]
34 /// Indicates IPC connection and data acquisition errors.
35 pub enum Error {
36     /// Successful.
37     Success,
38 
39     /// Creates MsgParcel Failed.
40     CreateMsgParcelFailed,
41 
42     /// Writes MsgParcel Failed.
43     WriteMsgParcelFailed,
44 
45     /// Reads MsgParcel Failed.
46     ReadMsgParcelFailed,
47 
48     /// Gets Proxy Object Failed.
49     GetProxyObjectFailed,
50 
51     /// Sends Request Failed.
52     SendRequestFailed,
53 
54     /// Unlock Failed.
55     UnlockFailed,
56 
57     /// Invalid Cloud Status.
58     InvalidCloudStatus,
59 
60     /// Invalid Space Status.
61     InvalidSpaceStatus,
62 
63     /// Downloads Failed.
64     DownloadFailed,
65 
66     /// Uploads Failed.
67     UploadFailed,
68 
69     /// Invalid Field Type Code
70     InvalidFieldType,
71 
72     /// Unknown error.
73     UnknownError,
74 
75     /// Network error.
76     NetworkError,
77 
78     /// Cloud is disabled.
79     CloudDisabled,
80 
81     /// The cloud database is locked by others.
82     LockedByOthers,
83 
84     /// The number of records exceeds the limit.
85     RecordLimitExceeded,
86 
87     /// The cloud has no space for the asset.
88     NoSpaceForAsset,
89 }
90 
91 impl Display for Error {
fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result92     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
93         match self {
94             Error::Success => write!(f, "Successful"),
95             Error::CreateMsgParcelFailed => write!(f, "Creates MsgParcel Failed"),
96             Error::WriteMsgParcelFailed => write!(f, "Writes MsgParcel Failed"),
97             Error::ReadMsgParcelFailed => write!(f, "Reads MsgParcel Failed"),
98             Error::GetProxyObjectFailed => write!(f, "Gets Proxy Object Failed"),
99             Error::SendRequestFailed => write!(f, "Sends Request Failed"),
100             Error::UnlockFailed => write!(f, "Unlock failed"),
101             Error::InvalidCloudStatus => write!(f, "Invalid Cloud Status"),
102             Error::InvalidSpaceStatus => write!(f, "Invalid Space Status"),
103             Error::DownloadFailed => write!(f, "Downloads Failed"),
104             Error::UploadFailed => write!(f, "Uploads Failed"),
105             Error::InvalidFieldType => write!(f, "Invalid Field Type Code"),
106             Error::UnknownError => write!(f, "Unknown error"),
107             Error::NetworkError => write!(f, "Network error"),
108             Error::CloudDisabled => write!(f, "Cloud is disabled"),
109             Error::LockedByOthers => write!(f, "The cloud database is locked by others"),
110             Error::RecordLimitExceeded => write!(f, "The number of records exceeds the limit"),
111             Error::NoSpaceForAsset => write!(f, "The cloud has no space for the asset"),
112         }
113     }
114 }
115 
116 impl std::error::Error for Error {}
117 
118 impl Deserialize for Error {
deserialize(parcel: &mut MsgParcel) -> IpcResult<Self>119     fn deserialize(parcel: &mut MsgParcel) -> IpcResult<Self> {
120         let index = parcel.read::<u32>()?;
121         match index {
122             0 => Ok(Error::Success),
123             1 => Ok(Error::CreateMsgParcelFailed),
124             2 => Ok(Error::WriteMsgParcelFailed),
125             3 => Ok(Error::ReadMsgParcelFailed),
126             4 => Ok(Error::GetProxyObjectFailed),
127             5 => Ok(Error::SendRequestFailed),
128             6 => Ok(Error::InvalidCloudStatus),
129             7 => Ok(Error::InvalidSpaceStatus),
130             8 => Ok(Error::DownloadFailed),
131             9 => Ok(Error::UploadFailed),
132             10 => Ok(Error::InvalidFieldType),
133             11 => Ok(Error::UnknownError),
134             12 => Ok(Error::NetworkError),
135             13 => Ok(Error::CloudDisabled),
136             14 => Ok(Error::LockedByOthers),
137             15 => Ok(Error::RecordLimitExceeded),
138             16 => Ok(Error::NoSpaceForAsset),
139             _ => Err(IpcStatusCode::Failed),
140         }
141     }
142 }
143