Skip to content

Commit e3d3933

Browse files
bsoyluoglufacebook-github-bot
authored andcommitted
Handle value and tensor errors as NSErrors keeping the underlying error codes (#9503)
Summary: Pull Request resolved: #9503 Change the value and tensor errors to NSErrors and to throw with ET error codes. Reviewed By: shoumikhin Differential Revision: D71592464
1 parent 9a6a393 commit e3d3933

File tree

4 files changed

+17
-43
lines changed

4 files changed

+17
-43
lines changed

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.mm

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ - (nullable instancetype)initWithTensor:(torch::executor::Tensor)tensor error:(N
5656
{
5757
if (tensor.scalar_type() != ScalarType::Float) {
5858
if (error) {
59-
*error = [ModelRuntimeValueErrorFactory invalidType:[NSString stringWithFormat:@"torch::executor::ScalarType::%hhd", tensor.scalar_type()] expectedType:@"torch::executor::ScalarType::Float"];
59+
*error = [NSError
60+
errorWithDomain:@"ExecutorchRuntimeEngine"
61+
code:(NSInteger)executorch::runtime::Error::InvalidArgument
62+
userInfo: @{NSDebugDescriptionErrorKey: [NSString stringWithFormat:@"Invalid type: torch::executor::ScalarType::%hhd, expected torch::executor::ScalarType::Float", tensor.scalar_type()]}];
6063
}
6164
return nil;
6265
}
@@ -90,9 +93,10 @@ - (nullable ModelRuntimeTensorValueBridgingTuple *)floatRepresentationAndReturnE
9093
}
9194

9295
if (error) {
93-
*error = [ModelRuntimeValueErrorFactory
94-
invalidType:[NSString stringWithFormat:@"torch::executor::ScalarType::%hhd", _tensor->scalar_type()]
95-
expectedType:@"torch::executor::ScalarType::Float"];
96+
*error = [NSError
97+
errorWithDomain:@"ExecutorchRuntimeEngine"
98+
code:(NSInteger)executorch::runtime::Error::InvalidArgument
99+
userInfo: @{NSDebugDescriptionErrorKey: [NSString stringWithFormat:@"Invalid type: torch::executor::ScalarType::%hhd, expected torch::executor::ScalarType::Float", _tensor->scalar_type()]}];
96100
}
97101

98102
return nil;

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.mm

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* This source code is licensed under the BSD-style license found in the
66
* LICENSE file in the root directory of this source tree.
77
*/
8-
8+
99
#import "ExecutorchRuntimeValue.h"
1010

1111
#import <map>
@@ -48,9 +48,10 @@ - (nullable ExecutorchRuntimeTensorValue *)asTensorValueAndReturnError:(NSError
4848
}
4949

5050
if (error) {
51-
*error = [ModelRuntimeValueErrorFactory
52-
invalidType:[NSString stringWithFormat:@"Tag::%d", _value.tag]
53-
expectedType:@"Tag::Tensor"];
51+
*error = [NSError
52+
errorWithDomain:@"ExecutorchRuntimeEngine"
53+
code:static_cast<uint32_t>(executorch::runtime::Error::InvalidArgument)
54+
userInfo: @{NSDebugDescriptionErrorKey: [NSString stringWithFormat:@"Invalid type: Tag::%d, expected Tag::Tensor", _value.tag]}];
5455
}
5556
return nil;
5657
}

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeValueTests.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ - (void)testTensorValueWithFloatArrayWithError
4646
NSError *error = nil;
4747
XCTAssertNil([[ExecutorchRuntimeTensorValue alloc] initWithTensor:*new torch::executor::Tensor(&tensorImpl) error:&error]);
4848
XCTAssertNotNil(error);
49-
XCTAssertEqualObjects([error description], @"Invalid type: torch::executor::ScalarType::3, expected torch::executor::ScalarType::Float");
49+
XCTAssertEqual(error.code, static_cast<uint32_t>(executorch::runtime::Error::InvalidArgument));
50+
XCTAssertEqualObjects(error.userInfo[NSDebugDescriptionErrorKey], @"Invalid type: torch::executor::ScalarType::3, expected torch::executor::ScalarType::Float");
5051
}
5152

5253
- (void)testTensorValueWithError
@@ -56,7 +57,8 @@ - (void)testTensorValueWithError
5657
NSError *error = nil;
5758
XCTAssertNil([value asTensorValueAndReturnError:&error]);
5859
XCTAssertNotNil(error);
59-
XCTAssertEqualObjects([error description], @"Invalid type: Tag::4, expected Tag::Tensor");
60+
XCTAssertEqual(error.code, static_cast<uint32_t>(executorch::runtime::Error::InvalidArgument));
61+
XCTAssertEqualObjects(error.userInfo[NSDebugDescriptionErrorKey], @"Invalid type: Tag::4, expected Tag::Tensor");
6062
}
6163

6264
@end

extension/apple/RuntimeBridgingCore/RuntimeBridgingCore/ModelRuntimeValueError.swift

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)