• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use std::io;
15 
16 use super::common::CommonError;
17 
18 #[derive(Debug)]
19 pub struct CacheDownloadError {
20     code: Option<i32>,
21     message: String,
22     kind: ErrorKind,
23 }
24 
25 impl CacheDownloadError {
code(&self) -> i3226     pub fn code(&self) -> i32 {
27         self.code.unwrap_or(0)
28     }
29 
message(&self) -> &str30     pub fn message(&self) -> &str {
31         &self.message
32     }
33 
ffi_kind(&self) -> i3234     pub fn ffi_kind(&self) -> i32 {
35         self.kind.clone() as i32
36     }
37 }
38 
39 #[derive(Debug, Clone)]
40 pub enum ErrorKind {
41     Http,
42     Io,
43 }
44 
45 impl From<io::Error> for CacheDownloadError {
from(err: io::Error) -> Self46     fn from(err: io::Error) -> Self {
47         CacheDownloadError {
48             code: err.raw_os_error(),
49             message: err.to_string(),
50             kind: ErrorKind::Io,
51         }
52     }
53 }
54 
55 impl<'a, E> From<&'a E> for CacheDownloadError
56 where
57     E: CommonError,
58 {
from(err: &'a E) -> Self59     fn from(err: &'a E) -> Self {
60         CacheDownloadError {
61             code: Some(err.code()),
62             message: err.msg().to_string(),
63             kind: ErrorKind::Http,
64         }
65     }
66 }
67