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::default; 15 use std::error::Error; 16 use std::fmt::{Debug, Display}; 17 18 use crate::wrapper::ffi; 19 20 pub struct HttpClientError { 21 code: HttpErrorCode, 22 msg: String, 23 } 24 25 impl HttpClientError { 26 pub(crate) fn from_ffi(inner: &ffi::HttpClientError) -> Self { 27 let code = HttpErrorCode::try_from(inner.GetErrorCode()) 28 .map_err(|e| {}) 29 .unwrap_or_default(); 30 let msg = inner.GetErrorMessage().to_string(); 31 Self { code, msg } 32 } 33 codenull34 pub fn code(&self) -> &HttpErrorCode { 35 &self.code 36 } 37 msgnull38 pub fn msg(&self) -> &str { 39 &self.msg 40 } 41 } 42 43 impl Error for HttpClientError {} 44 45 impl Display for HttpClientError { fmtnull46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 47 write!(f, "{:?}", self) 48 } 49 } 50 51 impl Debug for HttpClientError { 52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 53 write!( 54 f, 55 "HttpClientError: code {:?} msg {}", 56 self.code(), 57 self.msg() 58 ) 59 } 60 } 61 62 #[derive(Debug, Default)] 63 pub enum HttpErrorCode { 64 HttpNoneErr, 65 HttpPermissionDeniedCode = 201, 66 HttpParseErrorCode = 401, 67 HttpErrorCodeBase = 2300000, 68 HttpUnsupportedProtocol, 69 HttpFailedInit, 70 HttpUrlMalformat, 71 HttpCouldntResolveProxy = 2300005, 72 HttpCouldntResolveHost, 73 HttpCouldntConnect, 74 HttpWeirdServerReply, 75 HttpRemoteAccessDenied, 76 HttpHttp2Error = 2300016, 77 HttpPartialFile = 2300018, 78 HttpWriteError = 2300023, 79 HttpUploadFailed = 2300025, 80 HttpReadError = 2300026, 81 HttpOutOfMemory, 82 HttpOperationTimedout, 83 HttpPostError = 2300034, 84 HttpTaskCanceled = 2300042, 85 HttpTooManyRedirects = 2300047, 86 HttpGotNothing = 2300052, 87 HttpSendError = 2300055, 88 HttpRecvError, 89 HttpSslCertproblem = 2300058, 90 HttpSslCipher, 91 HttpPeerFailedVerification, 92 HttpBadContentEncoding, 93 HttpFilesizeExceeded = 2300063, 94 HttpRemoteDiskFull = 2300070, 95 HttpRemoteFileExists = 2300073, 96 HttpSslCacertBadfile = 2300077, 97 HttpRemoteFileNotFound, 98 HttpSslPinnedpubkeynotmatch = 2300090, 99 HttpAuthError = 2300094, 100 #[default] 101 HttpUnknownOtherError = 2300999, 102 } 103