3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #import <app/util/af-enums.h>
21 #import <core/CHIPError.h>
22 #import <inet/InetError.h>
24 NSString * const CHIPErrorDomain = @"CHIPErrorDomain";
26 @implementation CHIPError
28 + (NSError *)errorForCHIPErrorCode:(CHIP_ERROR)errorCode
31 case CHIP_ERROR_INVALID_STRING_LENGTH:
32 return [NSError errorWithDomain:CHIPErrorDomain
33 code:CHIPErrorCodeInvalidStringLength
34 userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"A list length is invalid.", nil) }];
35 case CHIP_ERROR_INVALID_INTEGER_VALUE:
36 return [NSError errorWithDomain:CHIPErrorDomain
37 code:CHIPErrorCodeInvalidIntegerValue
38 userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Unexpected integer value.", nil) }];
39 case CHIP_ERROR_INVALID_ARGUMENT:
40 return [NSError errorWithDomain:CHIPErrorDomain
41 code:CHIPErrorCodeInvalidArgument
42 userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"An argument is invalid.", nil) }];
43 case CHIP_ERROR_INVALID_MESSAGE_LENGTH:
44 return [NSError errorWithDomain:CHIPErrorDomain
45 code:CHIPErrorCodeInvalidMessageLength
46 userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"A message length is invalid.", nil) }];
47 case CHIP_ERROR_INCORRECT_STATE:
48 return [NSError errorWithDomain:CHIPErrorDomain
49 code:CHIPErrorCodeInvalidState
50 userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Invalid object state.", nil) }];
51 case CHIP_ERROR_INTEGRITY_CHECK_FAILED:
52 return [NSError errorWithDomain:CHIPErrorDomain
53 code:CHIPErrorCodeIntegrityCheckFailed
54 userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Integrity check failed.", nil) }];
55 case EMBER_ZCL_STATUS_DUPLICATE_EXISTS:
57 errorWithDomain:CHIPErrorDomain
58 code:CHIPErrorCodeDuplicateExists
59 userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"A Duplicate entry or setting exists.", nil) }];
61 return [NSError errorWithDomain:CHIPErrorDomain
63 userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Success.", nil) }];
65 return [NSError errorWithDomain:CHIPErrorDomain
66 code:CHIPErrorCodeUndefinedError
68 NSLocalizedDescriptionKey :
69 [NSString stringWithFormat:NSLocalizedString(@"Undefined error:%d.", nil), errorCode]
75 + (CHIP_ERROR)errorToCHIPErrorCode:(NSError *)error
77 if (error.domain != CHIPErrorDomain) {
78 return CHIP_ERROR_INTERNAL;
82 case CHIPErrorCodeDuplicateExists:
83 return EMBER_ZCL_STATUS_DUPLICATE_EXISTS;
84 case CHIPErrorCodeInvalidStringLength:
85 return CHIP_ERROR_INVALID_STRING_LENGTH;
86 case CHIPErrorCodeInvalidIntegerValue:
87 return CHIP_ERROR_INVALID_INTEGER_VALUE;
88 case CHIPErrorCodeInvalidArgument:
89 return CHIP_ERROR_INVALID_ARGUMENT;
90 case CHIPErrorCodeInvalidMessageLength:
91 return CHIP_ERROR_INVALID_MESSAGE_LENGTH;
92 case CHIPErrorCodeInvalidState:
93 return CHIP_ERROR_INCORRECT_STATE;
94 case CHIPErrorCodeIntegrityCheckFailed:
95 return CHIP_ERROR_INTEGRITY_CHECK_FAILED;
99 return CHIP_ERROR_INTERNAL;