1 /* Copyright 2022 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 16 #ifndef TENSORFLOW_CORE_TPU_TPU_EMBEDDING_CONFIGURATION_ERRORS_H_ 17 #define TENSORFLOW_CORE_TPU_TPU_EMBEDDING_CONFIGURATION_ERRORS_H_ 18 19 #include <string> 20 21 #include "absl/strings/str_cat.h" 22 #include "absl/strings/string_view.h" 23 #include "tensorflow/core/platform/status.h" 24 #include "tensorflow/core/platform/statusor.h" 25 #include "tensorflow/core/protobuf/tpu/tpu_embedding_configuration.pb.h" 26 27 namespace tensorflow::tpu { 28 29 // The payload URL for TPU embedding initialization permanent errors. 30 constexpr absl::string_view kTpuEmbeddingErrorUrl = 31 "type.googleapis.com/tensorflow.tpu.TPUEmbeddingError"; 32 33 constexpr absl::string_view kTpuEmbeddingErrorMessage = 34 "TPUEmbedding permanent error"; 35 36 // Appends a payload of type tensorflow::tpu::kTpuEmbeddingErrorUrl to the 37 // tensorflow::Status obj if the status is NOT OK. Returns the 38 // tensorflow::Status obj unchanged if the status is OK. 39 Status AppendTpuEmbeddingErrorPayload(Status obj); 40 41 // Appends a payload of type tensorflow::tpu::kTpuEmbeddingErrorUrl to the 42 // tensorflow::Status obj if the status is NOT OK. Returns obj.value() if the 43 // status is OK. 44 template <typename T> AppendTpuEmbeddingErrorPayload(StatusOr<T> obj)45StatusOr<T> AppendTpuEmbeddingErrorPayload(StatusOr<T> obj) { 46 if (obj.ok()) { 47 return std::move(obj.value()); 48 } else { 49 const std::string error_message = absl::StrCat( 50 kTpuEmbeddingErrorMessage, ". ", obj.status().error_message()); 51 Status status(obj.status().code(), error_message); 52 TPUEmbeddingError error_payload; 53 status.SetPayload(kTpuEmbeddingErrorUrl, error_payload.SerializeAsString()); 54 return status; 55 } 56 } 57 58 // Returns true if the tensorflow::Status obj has a payload of type 59 // tensorflow::tpu::kTpuEmbeddingErrorUrl. 60 bool HasTpuEmbeddingErrorPayload(const Status& status); 61 62 // Returns true if the tensorflow::Status obj error message contains 63 // tensorflow::tpu::kTpuEmbeddingErrorMessage as a substring. 64 bool HasTpuEmbeddingErrorMessage(const Status& status); 65 66 } // namespace tensorflow::tpu 67 68 #endif // TENSORFLOW_CORE_TPU_TPU_EMBEDDING_CONFIGURATION_ERRORS_H_ 69