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