xref: /aosp_15_r20/external/tensorflow/tensorflow/core/tfrt/utils/error_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_CORE_TFRT_UTILS_ERROR_UTIL_H_
16 #define TENSORFLOW_CORE_TFRT_UTILS_ERROR_UTIL_H_
17 
18 #include "tensorflow/core/platform/status.h"
19 #include "tfrt/support/error_util.h"  // from @tf_runtime
20 
21 namespace tfrt {
22 class AsyncValue;
23 class DecodedDiagnostic;
24 
25 tfrt::ErrorCode ConvertTfErrorCodeToTfrtErrorCode(
26     const tensorflow::Status& status);
27 
28 tensorflow::Status CreateTfErrorStatus(const DecodedDiagnostic& error);
29 
30 tensorflow::Status ToTfStatus(const AsyncValue* av);
31 
MakeStatusString(tensorflow::Status status)32 inline std::string MakeStatusString(tensorflow::Status status) {
33   switch (status.code()) {
34     case tensorflow::error::OK:
35       return "OK";
36     case tensorflow::error::CANCELLED:
37       return absl::StrCat("Cancelled: ", status.error_message());
38     case tensorflow::error::UNKNOWN:
39       return absl::StrCat("Unknown: ", status.error_message());
40     case tensorflow::error::INVALID_ARGUMENT:
41       return absl::StrCat("Invalid argument: ", status.error_message());
42     case tensorflow::error::DEADLINE_EXCEEDED:
43       return absl::StrCat("Deadline exceeded: ", status.error_message());
44     case tensorflow::error::NOT_FOUND:
45       return absl::StrCat("Not found: ", status.error_message());
46     case tensorflow::error::ALREADY_EXISTS:
47       return absl::StrCat("Already exists: ", status.error_message());
48     case tensorflow::error::PERMISSION_DENIED:
49       return absl::StrCat("Permission denied: ", status.error_message());
50     case tensorflow::error::UNAUTHENTICATED:
51       return absl::StrCat("Unauthenticated: ", status.error_message());
52     case tensorflow::error::RESOURCE_EXHAUSTED:
53       return absl::StrCat("Resource exhausted: ", status.error_message());
54     case tensorflow::error::FAILED_PRECONDITION:
55       return absl::StrCat("Failed precondition: ", status.error_message());
56     case tensorflow::error::ABORTED:
57       return absl::StrCat("Aborted: ", status.error_message());
58     case tensorflow::error::OUT_OF_RANGE:
59       return absl::StrCat("Out of range: ", status.error_message());
60     case tensorflow::error::UNIMPLEMENTED:
61       return absl::StrCat("Unimplemented: ", status.error_message());
62     case tensorflow::error::INTERNAL:
63       return absl::StrCat("Internal: ", status.error_message());
64     case tensorflow::error::UNAVAILABLE:
65       return absl::StrCat("Unavailable: ", status.error_message());
66     case tensorflow::error::DATA_LOSS:
67       return absl::StrCat("Data loss: ", status.error_message());
68     default:
69       return absl::StrCat("Unknown code: ", status.error_message());
70   }
71 }
72 
MakeStatusError(tensorflow::Status status)73 inline llvm::Error MakeStatusError(tensorflow::Status status) {
74   return MakeStringError(MakeStatusString(status));
75 }
76 
77 }  // namespace tfrt
78 
79 #endif  // TENSORFLOW_CORE_TFRT_UTILS_ERROR_UTIL_H_
80