diff --git a/mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.mm b/mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.mm index 9932dd13c..27b75515d 100644 --- a/mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.mm +++ b/mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.mm @@ -26,7 +26,7 @@ NSString *const MPPTasksErrorDomain = @"com.google.mediapipe.tasks"; namespace { - using absl::StatusCode; +using absl::StatusCode; } @implementation MPPCommonUtils @@ -72,95 +72,70 @@ namespace { return YES; } - // Creates the NSEror with the appropriate error - // MPPTasksErrorCode and message. MPPTasksErrorCode has a one to one - // mapping with MediaPipeTasksStatus starting from the value 1(MPPTasksErrorCodeError) - // and hence will be correctly initialized if directly cast from the integer code derived from - // MediaPipeTasksStatus stored in its payload. MPPTasksErrorCode omits kOk = 0 of - // MediaPipeTasksStatusx. - // - // Stores a string including absl status code and message(if non empty) as the - // error message See - // https://github.com/abseil/abseil-cpp/blob/master/absl/status/status.h#L514 - // for explanation. absl::Status::message() can also be used but not always - // guaranteed to be non empty. + /** Converts the absl status message to an NSString. */ NSString *description = [NSString stringWithCString:status.ToString(absl::StatusToStringMode::kWithNoExtraData).c_str() encoding:NSUTF8StringEncoding]; - - // Payload of absl::Status created by the MediaPipe task library stores an appropriate value of - // the enum MediaPipeTasksStatus. The integer value corresponding to the MediaPipeTasksStatus enum - // stored in the payload is extracted here to later map to the appropriate error code to be - // returned. In cases where the enum is not stored in (payload is NULL or the payload string - // cannot be converted to an integer), we set the error code value to be 1 - // (MPPTasksErrorCodeError of MPPTasksErrorCode used in the iOS library to signify - // any errors not falling into other categories.) Since payload is of type absl::Cord that can be - // type cast into an absl::optional, we use the std::stoi function to convert it into - // an integer code if possible. + MPPTasksErrorCode genericErrorCode = MPPTasksErrorCodeUnknownError; MPPTasksErrorCode errorCode = genericErrorCode; - // If errorCode is outside the range of enum values possible or is - // MPPTasksErrorCodeError, we try to map the absl::Status::code() to assign - // appropriate MPPTasksErrorCode in default cases. Note: - // The mapping to absl::Status::code() is done to generate a more specific error code than - // MPPTasksErrorCodeError in cases when the payload can't be mapped to - // MPPTasksErrorCode. This can happen when absl::Status returned by TFLite library are in turn - // returned without modification by MediaPipe cc library methods. - switch (status.code()) { - case StatusCode::kCancelled: - errorCode = MPPTasksErrorCodeCancelledError; - break; - case StatusCode::kUnknown: - errorCode = MPPTasksErrorCodeUnknownError; - break; - case StatusCode::kInvalidArgument: - errorCode = MPPTasksErrorCodeInvalidArgumentError; - break; - case StatusCode::kDeadlineExceeded: - errorCode = MPPTasksErrorCodeDeadlineExceededError; - break; - case StatusCode::kNotFound: - errorCode = MPPTasksErrorCodeNotFoundError; - break; - case StatusCode::kAlreadyExists: - errorCode = MPPTasksErrorCodeAlreadyExistsError; - break; - case StatusCode::kPermissionDenied: - errorCode = MPPTasksErrorCodePermissionDeniedError; - break; - case StatusCode::kResourceExhausted: - errorCode = MPPTasksErrorCodeResourceExhaustedError; - break; - case StatusCode::kFailedPrecondition: - errorCode = MPPTasksErrorCodeFailedPreconditionError; - break; - case StatusCode::kAborted: - errorCode = MPPTasksErrorCodeAbortedError; - break; - case StatusCode::kOutOfRange: - errorCode = MPPTasksErrorCodeOutOfRangeError; - break; - case StatusCode::kUnimplemented: - errorCode = MPPTasksErrorCodeUnimplementedError; - break; - case StatusCode::kInternal: - errorCode = MPPTasksErrorCodeInternalError; - break; - case StatusCode::kUnavailable: - errorCode = MPPTasksErrorCodeUnavailableError; - break; - case StatusCode::kDataLoss: - errorCode = MPPTasksErrorCodeDataLossError; - break; - case StatusCode::kUnauthenticated: - errorCode = MPPTasksErrorCodeUnauthenticatedError; - break; - default: - errorCode = genericErrorCode; - break; - } + /** Maps the absl::StatusCode to the appropriate MPPTasksErrorCode. Note: MPPTasksErrorCode omits + * absl::StatusCode::kOk. */ + switch (status.code()) { + case StatusCode::kCancelled: + errorCode = MPPTasksErrorCodeCancelledError; + break; + case StatusCode::kUnknown: + errorCode = MPPTasksErrorCodeUnknownError; + break; + case StatusCode::kInvalidArgument: + errorCode = MPPTasksErrorCodeInvalidArgumentError; + break; + case StatusCode::kDeadlineExceeded: + errorCode = MPPTasksErrorCodeDeadlineExceededError; + break; + case StatusCode::kNotFound: + errorCode = MPPTasksErrorCodeNotFoundError; + break; + case StatusCode::kAlreadyExists: + errorCode = MPPTasksErrorCodeAlreadyExistsError; + break; + case StatusCode::kPermissionDenied: + errorCode = MPPTasksErrorCodePermissionDeniedError; + break; + case StatusCode::kResourceExhausted: + errorCode = MPPTasksErrorCodeResourceExhaustedError; + break; + case StatusCode::kFailedPrecondition: + errorCode = MPPTasksErrorCodeFailedPreconditionError; + break; + case StatusCode::kAborted: + errorCode = MPPTasksErrorCodeAbortedError; + break; + case StatusCode::kOutOfRange: + errorCode = MPPTasksErrorCodeOutOfRangeError; + break; + case StatusCode::kUnimplemented: + errorCode = MPPTasksErrorCodeUnimplementedError; + break; + case StatusCode::kInternal: + errorCode = MPPTasksErrorCodeInternalError; + break; + case StatusCode::kUnavailable: + errorCode = MPPTasksErrorCodeUnavailableError; + break; + case StatusCode::kDataLoss: + errorCode = MPPTasksErrorCodeDataLossError; + break; + case StatusCode::kUnauthenticated: + errorCode = MPPTasksErrorCodeUnauthenticatedError; + break; + default: + errorCode = genericErrorCode; + break; + } [MPPCommonUtils createCustomError:error withCode:errorCode description:description]; return NO;