From 0b2a35e900ee8f64b2ebe463cac281bf8d439675 Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Thu, 12 Jun 2025 18:28:37 -0700 Subject: [PATCH] Add API to log errors with a custom description appended. (#11591) Summary: Pull Request resolved: https://github.com/pytorch/executorch/pull/11591 . Reviewed By: bsoyluoglu Differential Revision: D76482175 --- .../apple/ExecuTorch/Exported/ExecuTorchError.h | 12 ++++++++++++ .../apple/ExecuTorch/Exported/ExecuTorchError.m | 11 +++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchError.h b/extension/apple/ExecuTorch/Exported/ExecuTorchError.h index 15fbaee7472..4c6fa574588 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchError.h +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchError.h @@ -67,4 +67,16 @@ __attribute__((deprecated("This API is experimental."))) NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code) NS_SWIFT_NAME(Error(code:)); +/** + * Create an NSError in the ExecuTorch domain for the given code. + * + * @param code The ExecuTorchErrorCode to wrap. + * @param description Additional error description. + * @return An NSError with ExecuTorchErrorDomain, the specified code, and a localized description. + */ + FOUNDATION_EXPORT + __attribute__((deprecated("This API is experimental."))) + NSError *ExecuTorchErrorWithCodeAndDescription(ExecuTorchErrorCode code, NSString * __nullable description) + NS_SWIFT_NAME(Error(code:description:)); + NS_ASSUME_NONNULL_END diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchError.m b/extension/apple/ExecuTorch/Exported/ExecuTorchError.m index 2ecef863a2d..c54acb20e75 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchError.m +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchError.m @@ -54,9 +54,16 @@ } NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code) { + return ExecuTorchErrorWithCodeAndDescription(code, nil); +} + +NSError *ExecuTorchErrorWithCodeAndDescription(ExecuTorchErrorCode code, NSString * __nullable description) { return [NSError errorWithDomain:ExecuTorchErrorDomain code:code userInfo:@{ - NSLocalizedDescriptionKey : ExecuTorchErrorDescription(code) - }]; + NSLocalizedDescriptionKey: + description.length > 0 + ? [ExecuTorchErrorDescription(code) stringByAppendingFormat:@": %@", description] + : ExecuTorchErrorDescription(code) + }]; }