Skip to content

Commit 484b17e

Browse files
authored
Add helpers to create errors in ObjC/Swift
1 parent 12ed924 commit 484b17e

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorchError.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,62 @@ 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+
* NSError convenience initializer for ExecuTorch errors.
61+
*
62+
* @param code The ExecuTorchErrorCode to wrap into an NSError.
63+
* @return An NSError with ExecuTorchErrorDomain, the specified code, and a localized description.
64+
*/
65+
@interface NSError (ExecuTorch)
66+
67+
+ (instancetype)errorWithExecuTorchCode:(ExecuTorchErrorCode)code
68+
__attribute__((deprecated("This API is experimental.")))
69+
NS_SWIFT_NAME(init(code:));
70+
71+
@end
72+
1573
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)