Skip to content

Commit 10468d4

Browse files
committed
Added method for returning errors originating from C layer
1 parent 1911d65 commit 10468d4

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

tensorflow_lite_support/c/common_utils.cc

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ limitations under the License.
2424
namespace tflite {
2525
namespace support {
2626

27+
void CreateTfLiteSupportError(enum TfLiteSupportErrorCode code,
28+
const char* message, TfLiteSupportError** error) {
29+
if (error == nullptr) return;
30+
31+
*error = new TfLiteSupportError;
32+
(*error)->code = code;
33+
(*error)->message = strdup(message);
34+
}
35+
2736
void CreateTfLiteSupportErrorWithStatus(const absl::Status& status,
2837
TfLiteSupportError** error) {
2938
if (status.ok() or error == nullptr) return;
@@ -77,23 +86,41 @@ void CreateTfLiteSupportErrorWithStatus(const absl::Status& status,
7786
}
7887
}
7988

80-
*error = new TfLiteSupportError;
81-
// TfLiteErrorCode has a one to one mapping with TfLiteSupportStatus starting
82-
// from the value 1(kError) and hence will be correctly initialized if
83-
// directly cast from the integer code derived from TfLiteSupportStatus stored
84-
// in payload. TfLiteErrorCode omits kOk = 0 of TfLiteSupportStatus.
89+
// *error = new TfLiteSupportError;
90+
// // TfLiteErrorCode has a one to one mapping with TfLiteSupportStatus
91+
// starting
92+
// // from the value 1(kError) and hence will be correctly initialized if
93+
// // directly cast from the integer code derived from TfLiteSupportStatus
94+
// stored
95+
// // in payload. TfLiteErrorCode omits kOk = 0 of TfLiteSupportStatus.
96+
// //
97+
// (*error)->code = static_cast<TfLiteSupportErrorCode>(error_code);
98+
// // Stores a string including absl status code and message(if non empty) as
99+
// the
100+
// // error message See
101+
// //
102+
// https://github.com/abseil/abseil-cpp/blob/master/absl/status/status.h#L514
103+
// // for explanation. absl::Status::message() can also be used but not always
104+
// // guaranteed to be non empty.
105+
// (*error)->message = strdup(
106+
// status.ToString(absl::StatusToStringMode::kWithNoExtraData).c_str());
107+
108+
// Creates the TfLiteSupportError with the appropriate error
109+
// TfLiteSupportErrorCode and message. TfLiteErrorCode has a one to one
110+
// mapping with TfLiteSupportStatus starting from the value 1(kError) and
111+
// hence will be correctly initialized if directly cast from the integer code
112+
// derived from TfLiteSupportStatus stored in payload. TfLiteErrorCode omits
113+
// kOk = 0 of TfLiteSupportStatus.
85114
//
86-
// TODO(prianka): Investigate if switching between error code cast to
87-
// TfLiteSupportStatus and assigning appropriate value to TfLiteErrorCode is
88-
// necessary (If 'TfLiteSupportStatus' or other edge cases).
89-
(*error)->code = static_cast<TfLiteSupportErrorCode>(error_code);
90115
// Stores a string including absl status code and message(if non empty) as the
91116
// error message See
92117
// https://github.com/abseil/abseil-cpp/blob/master/absl/status/status.h#L514
93118
// for explanation. absl::Status::message() can also be used but not always
94119
// guaranteed to be non empty.
95-
(*error)->message = strdup(
96-
status.ToString(absl::StatusToStringMode::kWithNoExtraData).c_str());
120+
CreateTfLiteSupportError(
121+
static_cast<TfLiteSupportErrorCode>(error_code),
122+
status.ToString(absl::StatusToStringMode::kWithNoExtraData).c_str(),
123+
error);
97124
}
98125

99126
} // namespace support

tensorflow_lite_support/c/common_utils.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ limitations under the License.
2525
namespace tflite {
2626
namespace support {
2727

28+
// Creates a TfLiteSupportError with a TfLiteSupportErrorCode and message.
29+
void CreateTfLiteSupportError(enum TfLiteSupportErrorCode code,
30+
const char* message, TfLiteSupportError** error);
31+
2832
// Creates a TfLiteSupportError from absl::Status and passes it back as a
2933
// parameter which is a pointer to the error pointer.
3034
//
@@ -35,12 +39,12 @@ namespace support {
3539
// TfLiteSupportError **error) {
3640
// // Necessary checks
3741
// tflite::support::StatusOr<std::unique_ptr<ImageClassifier>> classifier_status
38-
// = // Call to create Cpp Image Classifier.
42+
// = // Call to create Cpp Image Classifier.
3943
// if (classifier_status.ok()) {
4044
// Code to return classifier
4145
// } else {
4246
// ::tflite::support::CreateTfLiteSupportErrorWithStatus(classifier_status.status(),
43-
// error);
47+
// error);
4448
// return nullptr;
4549
// }
4650
//}

0 commit comments

Comments
 (0)