forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTRError.mm
389 lines (352 loc) · 14.5 KB
/
MTRError.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
*
* Copyright (c) 2020 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.
*/
#import <Matter/MTRDefines.h>
#import "MTRError.h"
#import "MTRError_Internal.h"
#import "MTRLogging_Internal.h"
#import <app/MessageDef/StatusIB.h>
#import <inet/InetError.h>
#import <lib/support/TypeTraits.h>
#import <objc/runtime.h>
NSString * const MTRErrorDomain = @"MTRErrorDomain";
NSString * const MTRInteractionErrorDomain = @"MTRInteractionErrorDomain";
// Class for holding on to a CHIP_ERROR that we can use as the value
// in a dictionary.
@interface MTRErrorHolder : NSObject
@property (nonatomic, readonly) CHIP_ERROR error;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)initWithError:(CHIP_ERROR)error;
@end
@implementation MTRError
+ (NSError *)errorWithCode:(MTRErrorCode)code
{
return [NSError errorWithDomain:MTRErrorDomain code:code userInfo:nil];
}
+ (NSError *)errorForCHIPErrorCode:(CHIP_ERROR)errorCode
{
return [MTRError errorForCHIPErrorCode:errorCode logContext:nil];
}
+ (NSError *)errorForCHIPErrorCode:(CHIP_ERROR)errorCode logContext:(id)contextToLog
{
if (errorCode == CHIP_NO_ERROR) {
return nil;
}
ChipLogError(Controller, "Creating NSError from %" CHIP_ERROR_FORMAT " (context: %@)", errorCode.Format(), contextToLog);
if (errorCode.IsIMStatus()) {
chip::app::StatusIB status(errorCode);
return [MTRError errorForIMStatus:status];
}
MTRErrorCode code;
NSString * description;
NSDictionary * additionalUserInfo;
switch (errorCode.AsInteger()) {
case CHIP_ERROR_INVALID_STRING_LENGTH.AsInteger():
code = MTRErrorCodeInvalidStringLength;
description = NSLocalizedString(@"A list length is invalid.", nil);
break;
case CHIP_ERROR_INVALID_INTEGER_VALUE.AsInteger():
code = MTRErrorCodeInvalidIntegerValue;
description = NSLocalizedString(@"Unexpected integer value.", nil);
break;
case CHIP_ERROR_INVALID_ARGUMENT.AsInteger():
code = MTRErrorCodeInvalidArgument;
description = NSLocalizedString(@"An argument is invalid.", nil);
break;
case CHIP_ERROR_INVALID_MESSAGE_LENGTH.AsInteger():
code = MTRErrorCodeInvalidMessageLength;
description = NSLocalizedString(@"A message length is invalid.", nil);
break;
case CHIP_ERROR_INCORRECT_STATE.AsInteger():
code = MTRErrorCodeInvalidState;
description = NSLocalizedString(@"Invalid object state.", nil);
break;
case CHIP_ERROR_INTEGRITY_CHECK_FAILED.AsInteger():
code = MTRErrorCodeIntegrityCheckFailed;
description = NSLocalizedString(@"Integrity check failed.", nil);
break;
case CHIP_ERROR_TIMEOUT.AsInteger():
code = MTRErrorCodeTimeout;
description = NSLocalizedString(@"Transaction timed out.", nil);
break;
case CHIP_ERROR_BUFFER_TOO_SMALL.AsInteger():
code = MTRErrorCodeBufferTooSmall;
description = NSLocalizedString(@"A buffer is too small.", nil);
break;
case CHIP_ERROR_FABRIC_EXISTS.AsInteger():
code = MTRErrorCodeFabricExists;
description = NSLocalizedString(@"The device is already a member of this fabric.", nil);
break;
case CHIP_ERROR_SCHEMA_MISMATCH.AsInteger():
code = MTRErrorCodeSchemaMismatch;
description = NSLocalizedString(@"Data does not match expected schema.", nil);
break;
case CHIP_ERROR_DECODE_FAILED.AsInteger():
code = MTRErrorCodeTLVDecodeFailed;
description = NSLocalizedString(@"TLV decoding failed.", nil);
break;
case CHIP_ERROR_DNS_SD_UNAUTHORIZED.AsInteger():
code = MTRErrorCodeDNSSDUnauthorized;
description = NSLocalizedString(@"Access denied to perform DNS-SD lookups. "
"Check that \"_matter._tcp\" and/or \"_matterc._udp\" "
"are listed under the NSBonjourServices key in Info.plist",
nil);
break;
case CHIP_ERROR_CANCELLED.AsInteger():
code = MTRErrorCodeCancelled;
description = NSLocalizedString(@"The operation was cancelled.", nil);
break;
case CHIP_ERROR_ACCESS_DENIED.AsInteger():
code = MTRErrorCodeAccessDenied;
description = NSLocalizedString(@"Access denied.", nil);
break;
case CHIP_ERROR_BUSY.AsInteger():
code = MTRErrorCodeBusy;
description = NSLocalizedString(@"Operation cannot be completed at this time: resource busy.", nil);
break;
case CHIP_ERROR_NOT_FOUND.AsInteger():
code = MTRErrorCodeNotFound;
description = NSLocalizedString(@"Requested resource was not found.", nil);
break;
default:
code = MTRErrorCodeGeneralError;
description = [NSString stringWithFormat:NSLocalizedString(@"General error: %u", nil), errorCode.AsInteger()];
additionalUserInfo = @{ @"errorCode" : @(errorCode.AsInteger()) };
}
NSDictionary * userInfo = @{ NSLocalizedDescriptionKey : description };
if (additionalUserInfo) {
NSMutableDictionary * combined = [userInfo mutableCopy];
[combined addEntriesFromDictionary:additionalUserInfo];
userInfo = combined;
}
auto * error = [NSError errorWithDomain:MTRErrorDomain code:code userInfo:userInfo];
void * key = (__bridge void *) [MTRErrorHolder class];
objc_setAssociatedObject(error, key, [[MTRErrorHolder alloc] initWithError:errorCode], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return error;
}
+ (NSError *)errorForCHIPIntegerCode:(uint32_t)errorCode
{
return [MTRError errorForCHIPErrorCode:chip::ChipError(errorCode)];
}
+ (NSError *)errorForIMStatus:(const chip::app::StatusIB &)status
{
if (status.IsSuccess()) {
return nil;
}
NSString * description;
using chip::Protocols::InteractionModel::Status;
switch (status.mStatus) {
case Status::Failure:
default:
description = NSLocalizedString(@"Operation was not successful.", nil);
break;
case Status::InvalidSubscription:
description = NSLocalizedString(@"Subscription ID is not active.", nil);
break;
case Status::UnsupportedAccess:
description = NSLocalizedString(@"The sender of the action or command does not have authorization or access.", nil);
break;
case Status::UnsupportedEndpoint:
description = NSLocalizedString(@"The endpoint indicated is unsupported on the node.", nil);
break;
case Status::InvalidAction:
description = NSLocalizedString(@"The action is malformed, has missing fields, or fields with invalid values. "
"Action not carried out.",
nil);
break;
case Status::UnsupportedCommand:
description = NSLocalizedString(@"The specified action or command indicated is not supported on the device."
"Command or action not carried out.",
nil);
break;
case Status::InvalidCommand:
description = NSLocalizedString(@"The cluster command is malformed, has missing fields, or fields with invalid values."
"Command not carried out.",
nil);
break;
case Status::UnsupportedAttribute:
description = NSLocalizedString(@"The specified attribute or attribute data field or entry does not exist on the device.", nil);
break;
case Status::ConstraintError:
description = NSLocalizedString(@"Out of range error or set to a reserved value.", nil);
break;
case Status::UnsupportedWrite:
description = NSLocalizedString(@"Attempt to write a read-only attribute.", nil);
break;
case Status::ResourceExhausted:
description = NSLocalizedString(@"An action or operation failed due to insufficient available resources. ", nil);
break;
case Status::NotFound:
description = NSLocalizedString(@"The indicated data field or entry could not be found.", nil);
break;
case Status::UnreportableAttribute:
description = NSLocalizedString(@"Reports cannot be issued for this attribute.", nil);
break;
case Status::InvalidDataType:
description = NSLocalizedString(@"The data type indicated is undefined or invalid for the indicated data field. "
"Command or action not carried out.",
nil);
break;
case Status::UnsupportedRead:
description = NSLocalizedString(@"Attempt to read a write-only attribute.", nil);
break;
case Status::DataVersionMismatch:
description = NSLocalizedString(@"Cluster instance data version did not match request path.", nil);
break;
case Status::Timeout:
description = NSLocalizedString(@"The transaction was aborted due to time being exceeded.", nil);
break;
case Status::Busy: {
description = NSLocalizedString(@"The receiver is busy processing another action "
"that prevents the execution of the incoming action.",
nil);
break;
case Status::UnsupportedCluster:
description = NSLocalizedString(@"The cluster indicated is not supported", nil);
break;
// Gap in values is intentional.
case Status::NoUpstreamSubscription:
description = NSLocalizedString(@"Proxy does not have a subscription to the source.", nil);
break;
}
case Status::NeedsTimedInteraction:
description = NSLocalizedString(@"An Untimed Write or Untimed Invoke interaction was used "
"for an attribute or command that requires a Timed Write or Timed Invoke.",
nil);
break;
case Status::UnsupportedEvent:
description = NSLocalizedString(@"The event indicated is unsupported on the cluster.", nil);
break;
}
NSMutableDictionary * userInfo = [[NSMutableDictionary alloc] init];
userInfo[NSLocalizedDescriptionKey] = description;
if (status.mClusterStatus.HasValue()) {
userInfo[@"clusterStatus"] = @(status.mClusterStatus.Value());
}
return [NSError errorWithDomain:MTRInteractionErrorDomain code:chip::to_underlying(status.mStatus) userInfo:userInfo];
}
+ (NSError *)errorForIMStatusCode:(chip::Protocols::InteractionModel::Status)status
{
return [self errorForIMStatus:chip::app::StatusIB(status)];
}
+ (CHIP_ERROR)errorToCHIPErrorCode:(NSError * _Nullable)error
{
if (error == nil) {
return CHIP_NO_ERROR;
}
if (error.domain == MTRInteractionErrorDomain) {
chip::app::StatusIB status(static_cast<chip::Protocols::InteractionModel::Status>(error.code));
if (error.userInfo != nil && error.userInfo[@"clusterStatus"] != nil) {
status.mClusterStatus.Emplace([error.userInfo[@"clusterStatus"] unsignedCharValue]);
}
return status.ToChipError();
}
if (error.domain != MTRErrorDomain) {
ChipLogError(Controller, "Trying to convert non-Matter error %@ to a Matter error code", error);
return CHIP_ERROR_INTERNAL;
}
{
void * key = (__bridge void *) [MTRErrorHolder class];
id underlyingError = objc_getAssociatedObject(error, key);
if (underlyingError != nil && [underlyingError isKindOfClass:[MTRErrorHolder class]]) {
return ((MTRErrorHolder *) underlyingError).error;
}
}
chip::ChipError::StorageType code;
switch (error.code) {
case MTRErrorCodeInvalidStringLength:
code = CHIP_ERROR_INVALID_STRING_LENGTH.AsInteger();
break;
case MTRErrorCodeInvalidIntegerValue:
code = CHIP_ERROR_INVALID_INTEGER_VALUE.AsInteger();
break;
case MTRErrorCodeInvalidArgument:
code = CHIP_ERROR_INVALID_ARGUMENT.AsInteger();
break;
case MTRErrorCodeInvalidMessageLength:
code = CHIP_ERROR_INVALID_MESSAGE_LENGTH.AsInteger();
break;
case MTRErrorCodeInvalidState:
code = CHIP_ERROR_INCORRECT_STATE.AsInteger();
break;
case MTRErrorCodeIntegrityCheckFailed:
code = CHIP_ERROR_INTEGRITY_CHECK_FAILED.AsInteger();
break;
case MTRErrorCodeTimeout:
code = CHIP_ERROR_TIMEOUT.AsInteger();
break;
case MTRErrorCodeBufferTooSmall:
code = CHIP_ERROR_BUFFER_TOO_SMALL.AsInteger();
break;
case MTRErrorCodeFabricExists:
code = CHIP_ERROR_FABRIC_EXISTS.AsInteger();
break;
case MTRErrorCodeSchemaMismatch:
code = CHIP_ERROR_SCHEMA_MISMATCH.AsInteger();
break;
case MTRErrorCodeTLVDecodeFailed:
code = CHIP_ERROR_DECODE_FAILED.AsInteger();
break;
case MTRErrorCodeDNSSDUnauthorized:
code = CHIP_ERROR_DNS_SD_UNAUTHORIZED.AsInteger();
break;
case MTRErrorCodeCancelled:
code = CHIP_ERROR_CANCELLED.AsInteger();
break;
case MTRErrorCodeAccessDenied:
code = CHIP_ERROR_ACCESS_DENIED.AsInteger();
break;
case MTRErrorCodeBusy:
code = CHIP_ERROR_BUSY.AsInteger();
break;
case MTRErrorCodeNotFound:
code = CHIP_ERROR_NOT_FOUND.AsInteger();
break;
case MTRErrorCodeGeneralError: {
id userInfoErrorCode = error.userInfo[@"errorCode"];
if ([userInfoErrorCode isKindOfClass:NSNumber.class]) {
code = static_cast<decltype(code)>([userInfoErrorCode unsignedLongValue]);
break;
}
// Weird error we did not create. Fall through.
}
default:
code = CHIP_ERROR_INTERNAL.AsInteger();
break;
}
return chip::ChipError(code);
}
+ (uint32_t)errorToCHIPIntegerCode:(NSError * _Nullable)error
{
return [self errorToCHIPErrorCode:error].AsInteger();
}
@end
@implementation MTRErrorHolder
- (instancetype)initWithError:(CHIP_ERROR)error
{
if (!(self = [super init])) {
return nil;
}
_error = error;
return self;
}
@end
void MTRThrowInvalidArgument(NSString * reason)
{
MTR_LOG_ERROR("Invalid argument: %@", reason);
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:reason userInfo:nil];
}