Skip to content

Commit 2b7dfe2

Browse files
authored
Add API to log errors with a custom description appended.
Differential Revision: D76482175 Pull Request resolved: pytorch/executorch#11591
1 parent f5b711f commit 2b7dfe2

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorchError.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,16 @@ __attribute__((deprecated("This API is experimental.")))
6767
NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code)
6868
NS_SWIFT_NAME(Error(code:));
6969

70+
/**
71+
* Create an NSError in the ExecuTorch domain for the given code.
72+
*
73+
* @param code The ExecuTorchErrorCode to wrap.
74+
* @param description Additional error description.
75+
* @return An NSError with ExecuTorchErrorDomain, the specified code, and a localized description.
76+
*/
77+
FOUNDATION_EXPORT
78+
__attribute__((deprecated("This API is experimental.")))
79+
NSError *ExecuTorchErrorWithCodeAndDescription(ExecuTorchErrorCode code, NSString * __nullable description)
80+
NS_SWIFT_NAME(Error(code:description:));
81+
7082
NS_ASSUME_NONNULL_END

extension/apple/ExecuTorch/Exported/ExecuTorchError.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,16 @@
5454
}
5555

5656
NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code) {
57+
return ExecuTorchErrorWithCodeAndDescription(code, nil);
58+
}
59+
60+
NSError *ExecuTorchErrorWithCodeAndDescription(ExecuTorchErrorCode code, NSString * __nullable description) {
5761
return [NSError errorWithDomain:ExecuTorchErrorDomain
5862
code:code
5963
userInfo:@{
60-
NSLocalizedDescriptionKey : ExecuTorchErrorDescription(code)
61-
}];
64+
NSLocalizedDescriptionKey:
65+
description.length > 0
66+
? [ExecuTorchErrorDescription(code) stringByAppendingFormat:@": %@", description]
67+
: ExecuTorchErrorDescription(code)
68+
}];
6269
}

0 commit comments

Comments
 (0)