@@ -24,6 +24,15 @@ limitations under the License.
2424namespace tflite {
2525namespace 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+
2736void 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
0 commit comments