1 // Copyright (C) 2023 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 core::fmt; 15 use std::io; 16 17 #[derive(Clone, Copy, PartialEq, Debug)] 18 pub(crate) enum ErrorCode { 19 ErrOk = 0, 20 #[allow(dead_code)] 21 IpcSizeTooLarge = 2, 22 ChannelNotOpen = 5, 23 Permission = 201, 24 SystemApi = 202, 25 ParameterCheck = 401, 26 FileOperationErr = 13400001, 27 Other = 13499999, 28 TaskEnqueueErr = 21900004, 29 TaskModeErr = 21900005, 30 TaskNotFound = 21900006, 31 TaskStateErr = 21900007, 32 GroupNotFound = 21900008, 33 } 34 35 impl From<ServiceError> for ErrorCode { from(value: ServiceError) -> Self36 fn from(value: ServiceError) -> Self { 37 match value { 38 ServiceError::IoError(_error) => ErrorCode::FileOperationErr, 39 ServiceError::ErrorCode(error_code) => error_code, 40 } 41 } 42 } 43 44 #[derive(Debug)] 45 pub(crate) enum ServiceError { 46 IoError(io::Error), 47 ErrorCode(ErrorCode), 48 } 49 50 impl Clone for ServiceError { clone(&self) -> Self51 fn clone(&self) -> Self { 52 match self { 53 Self::IoError(arg0) => Self::IoError(io::Error::new(arg0.kind(), arg0.to_string())), 54 Self::ErrorCode(arg0) => Self::ErrorCode(*arg0), 55 } 56 } 57 } 58 59 impl std::error::Error for ServiceError {} 60 61 impl fmt::Display for ServiceError { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 63 write!(f, "{:?}", self) 64 } 65 } 66 67 #[cfg(test)] 68 mod ut_error { 69 include!("../tests/ut/ut_error.rs"); 70 } 71