Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / darwin / Framework / CHIP / CHIPError.mm
1 /**
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #import "CHIPError.h"
19
20 #import <app/util/af-enums.h>
21 #import <core/CHIPError.h>
22 #import <inet/InetError.h>
23
24 NSString * const CHIPErrorDomain = @"CHIPErrorDomain";
25
26 @implementation CHIPError
27
28 + (NSError *)errorForCHIPErrorCode:(CHIP_ERROR)errorCode
29 {
30     switch (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:
56         return [NSError
57             errorWithDomain:CHIPErrorDomain
58                        code:CHIPErrorCodeDuplicateExists
59                    userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"A Duplicate entry or setting exists.", nil) }];
60     case CHIP_NO_ERROR:
61         return [NSError errorWithDomain:CHIPErrorDomain
62                                    code:CHIPSuccess
63                                userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Success.", nil) }];
64     default:
65         return [NSError errorWithDomain:CHIPErrorDomain
66                                    code:CHIPErrorCodeUndefinedError
67                                userInfo:@{
68                                    NSLocalizedDescriptionKey :
69                                        [NSString stringWithFormat:NSLocalizedString(@"Undefined error:%d.", nil), errorCode]
70                                }];
71         ;
72     }
73 }
74
75 + (CHIP_ERROR)errorToCHIPErrorCode:(NSError *)error
76 {
77     if (error.domain != CHIPErrorDomain) {
78         return CHIP_ERROR_INTERNAL;
79     }
80
81     switch (error.code) {
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;
96     case CHIPSuccess:
97         return CHIP_NO_ERROR;
98     default:
99         return CHIP_ERROR_INTERNAL;
100     }
101 }
102 @end