/* * * Copyright (c) 2020-2022 Project CHIP Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use core::{array::TryFromSliceError, fmt, str::Utf8Error}; #[derive(Debug, PartialEq, Clone, Copy)] pub enum Error { AttributeNotFound, AttributeIsCustom, BufferTooSmall, ClusterNotFound, CommandNotFound, Duplicate, EndpointNotFound, InvalidAction, InvalidCommand, InvalidDataType, UnsupportedAccess, ResourceExhausted, Busy, DataVersionMismatch, Crypto, TLSStack, MdnsError, Network, NoCommand, NoEndpoint, NoExchange, NoFabricId, NoHandler, NoNetworkInterface, NoNodeId, NoMemory, NoSession, NoSpace, NoSpaceAckTable, NoSpaceRetransTable, NoTagFound, NotFound, PacketPoolExhaust, StdIoError, SysTimeFail, Invalid, InvalidAAD, InvalidData, InvalidKeyLength, InvalidOpcode, InvalidPeerAddr, // Invalid Auth Key in the Matter Certificate InvalidAuthKey, InvalidSignature, InvalidState, InvalidTime, InvalidArgument, RwLock, TLVNotFound, TLVTypeMismatch, TruncatedPacket, Utf8Fail, } impl Error { pub fn remap(self, matcher: F, to: Self) -> Self where F: FnOnce(&Self) -> bool, { if matcher(&self) { to } else { self } } pub fn map_invalid(self, to: Self) -> Self { self.remap(|e| matches!(e, Self::Invalid | Self::InvalidData), to) } pub fn map_invalid_command(self) -> Self { self.map_invalid(Error::InvalidCommand) } pub fn map_invalid_action(self) -> Self { self.map_invalid(Error::InvalidAction) } pub fn map_invalid_data_type(self) -> Self { self.map_invalid(Error::InvalidDataType) } } #[cfg(feature = "std")] impl From for Error { fn from(_e: std::io::Error) -> Self { // Keep things simple for now Self::StdIoError } } #[cfg(feature = "std")] impl From> for Error { fn from(_e: std::sync::PoisonError) -> Self { Self::RwLock } } #[cfg(feature = "crypto_openssl")] impl From for Error { fn from(e: openssl::error::ErrorStack) -> Self { ::log::error!("Error in TLS: {}", e); Self::TLSStack } } #[cfg(feature = "crypto_mbedtls")] impl From for Error { fn from(e: mbedtls::Error) -> Self { ::log::error!("Error in TLS: {}", e); Self::TLSStack } } #[cfg(feature = "crypto_rustcrypto")] impl From for Error { fn from(_e: ccm::aead::Error) -> Self { Self::Crypto } } #[cfg(feature = "std")] impl From for Error { fn from(_e: std::time::SystemTimeError) -> Self { Self::SysTimeFail } } impl From for Error { fn from(_e: TryFromSliceError) -> Self { Self::Invalid } } impl From for Error { fn from(_e: Utf8Error) -> Self { Self::Utf8Fail } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self) } } #[cfg(feature = "std")] impl std::error::Error for Error {}