Skip to content

Commit 9a18669

Browse files
authored
Add helpers to create errors in ObjC/Swift (#10575)
1 parent c02dfd6 commit 9a18669

File tree

4 files changed

+111
-19
lines changed

4 files changed

+111
-19
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorchError.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,57 @@ NS_ASSUME_NONNULL_BEGIN
1212

1313
FOUNDATION_EXPORT NSErrorDomain const ExecuTorchErrorDomain NS_SWIFT_NAME(ErrorDomain);
1414

15+
/**
16+
* Enum to define the error codes.
17+
* Values can be a subset, but must numerically match exactly those defined in
18+
* runtime/core/error.h
19+
*/
20+
typedef NS_ERROR_ENUM(ExecuTorchErrorDomain, ExecuTorchErrorCode) {
21+
// System errors.
22+
ExecuTorchErrorCodeOk = 0,
23+
ExecuTorchErrorCodeInternal = 1,
24+
ExecuTorchErrorCodeInvalidState = 2,
25+
ExecuTorchErrorCodeEndOfMethod = 3,
26+
27+
// Logical errors.
28+
ExecuTorchErrorCodeNotSupported = 16,
29+
ExecuTorchErrorCodeNotImplemented = 17,
30+
ExecuTorchErrorCodeInvalidArgument = 18,
31+
ExecuTorchErrorCodeInvalidType = 19,
32+
ExecuTorchErrorCodeOperatorMissing = 20,
33+
34+
// Resource errors.
35+
ExecuTorchErrorCodeNotFound = 32,
36+
ExecuTorchErrorCodeMemoryAllocationFailed = 33,
37+
ExecuTorchErrorCodeAccessFailed = 34,
38+
ExecuTorchErrorCodeInvalidProgram = 35,
39+
ExecuTorchErrorCodeInvalidExternalData = 36,
40+
ExecuTorchErrorCodeOutOfResources = 37,
41+
42+
// Delegate errors.
43+
ExecuTorchErrorCodeDelegateInvalidCompatibility = 48,
44+
ExecuTorchErrorCodeDelegateMemoryAllocationFailed = 49,
45+
ExecuTorchErrorCodeDelegateInvalidHandle = 50,
46+
} NS_SWIFT_NAME(ErrorCode);
47+
48+
/**
49+
* Returns a brief error description for the given error code.
50+
*
51+
* @param code An ExecuTorchErrorCode value representing the error code.
52+
* @return An NSString containing the error description.
53+
*/
54+
FOUNDATION_EXPORT
55+
__attribute__((deprecated("This API is experimental.")))
56+
NSString *ExecuTorchErrorDescription(ExecuTorchErrorCode code)
57+
NS_SWIFT_NAME(ErrorDescription(_:));
58+
59+
/**
60+
* Create an NSError in the ExecuTorch domain for the given code.
61+
*
62+
* @param code The ExecuTorchErrorCode to wrap.
63+
* @return An NSError with ExecuTorchErrorDomain, the specified code, and a localized description.
64+
*/
65+
FOUNDATION_EXPORT NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code)
66+
NS_SWIFT_NAME(Error(code:));
67+
1568
NS_ASSUME_NONNULL_END

extension/apple/ExecuTorch/Exported/ExecuTorchError.m

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,54 @@
99
#import "ExecuTorchError.h"
1010

1111
NSErrorDomain const ExecuTorchErrorDomain = @"org.pytorch.executorch.error";
12+
13+
NSString *ExecuTorchErrorDescription(ExecuTorchErrorCode code) {
14+
switch (code) {
15+
case ExecuTorchErrorCodeOk:
16+
return @"";
17+
case ExecuTorchErrorCodeInternal:
18+
return @"Internal error";
19+
case ExecuTorchErrorCodeInvalidState:
20+
return @"Invalid executor state";
21+
case ExecuTorchErrorCodeEndOfMethod:
22+
return @"No more execution steps";
23+
case ExecuTorchErrorCodeNotSupported:
24+
return @"Operation not supported";
25+
case ExecuTorchErrorCodeNotImplemented:
26+
return @"Operation not implemented";
27+
case ExecuTorchErrorCodeInvalidArgument:
28+
return @"Invalid argument";
29+
case ExecuTorchErrorCodeInvalidType:
30+
return @"Invalid type";
31+
case ExecuTorchErrorCodeOperatorMissing:
32+
return @"Operator missing";
33+
case ExecuTorchErrorCodeNotFound:
34+
return @"Resource not found";
35+
case ExecuTorchErrorCodeMemoryAllocationFailed:
36+
return @"Memory allocation failed";
37+
case ExecuTorchErrorCodeAccessFailed:
38+
return @"Access failed";
39+
case ExecuTorchErrorCodeInvalidProgram:
40+
return @"Invalid program contents";
41+
case ExecuTorchErrorCodeInvalidExternalData:
42+
return @"Invalid external data";
43+
case ExecuTorchErrorCodeOutOfResources:
44+
return @"Out of resources";
45+
case ExecuTorchErrorCodeDelegateInvalidCompatibility:
46+
return @"Delegate version incompatible";
47+
case ExecuTorchErrorCodeDelegateMemoryAllocationFailed:
48+
return @"Delegate memory allocation failed";
49+
case ExecuTorchErrorCodeDelegateInvalidHandle:
50+
return @"Delegate handle invalid";
51+
default:
52+
return @"Unknown error";
53+
}
54+
}
55+
56+
NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code) {
57+
return [NSError errorWithDomain:ExecuTorchErrorDomain
58+
code:code
59+
userInfo:@{
60+
NSLocalizedDescriptionKey : ExecuTorchErrorDescription(code)
61+
}];
62+
}

extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ - (BOOL)loadWithVerification:(ExecuTorchVerification)verification
8787
const auto errorCode = _module->load(static_cast<Program::Verification>(verification));
8888
if (errorCode != Error::Ok) {
8989
if (error) {
90-
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
91-
code:(NSInteger)errorCode
92-
userInfo:nil];
90+
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode);
9391
}
9492
return NO;
9593
}
@@ -110,9 +108,7 @@ - (BOOL)loadMethod:(NSString *)methodName
110108
const auto errorCode = _module->load_method(methodName.UTF8String);
111109
if (errorCode != Error::Ok) {
112110
if (error) {
113-
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
114-
code:(NSInteger)errorCode
115-
userInfo:nil];
111+
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode);
116112
}
117113
return NO;
118114
}
@@ -127,9 +123,7 @@ - (BOOL)isMethodLoaded:(NSString *)methodName {
127123
const auto result = _module->method_names();
128124
if (!result.ok()) {
129125
if (error) {
130-
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
131-
code:(NSInteger)result.error()
132-
userInfo:nil];
126+
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error());
133127
}
134128
return nil;
135129
}
@@ -151,9 +145,7 @@ - (BOOL)isMethodLoaded:(NSString *)methodName {
151145
const auto result = _module->execute(methodName.UTF8String, inputs);
152146
if (!result.ok()) {
153147
if (error) {
154-
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
155-
code:(NSInteger)result.error()
156-
userInfo:nil];
148+
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error());
157149
}
158150
return nil;
159151
}

extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,10 @@ - (void)mutableBytesWithHandler:(void (^)(void *pointer, NSInteger count, ExecuT
112112

113113
- (BOOL)resizeToShape:(NSArray<NSNumber *> *)shape
114114
error:(NSError **)error {
115-
const auto resizeError = resize_tensor_ptr(
116-
_tensor, utils::toVector<SizesType>(shape)
117-
);
118-
if (resizeError != Error::Ok) {
115+
const auto errorCode = resize_tensor_ptr(_tensor, utils::toVector<SizesType>(shape));
116+
if (errorCode != Error::Ok) {
119117
if (error) {
120-
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
121-
code:(NSInteger)resizeError
122-
userInfo:nil];
118+
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode);
123119
}
124120
return NO;
125121
}

0 commit comments

Comments
 (0)