Home | History | Annotate | Download | only in core
      1 /* Copyright 2015 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_LIB_CORE_ERRORS_H_
     17 #define TENSORFLOW_LIB_CORE_ERRORS_H_
     18 
     19 #include "tensorflow/core/lib/core/status.h"
     20 #include "tensorflow/core/lib/strings/strcat.h"
     21 #include "tensorflow/core/platform/logging.h"
     22 #include "tensorflow/core/platform/macros.h"
     23 
     24 namespace tensorflow {
     25 namespace errors {
     26 
     27 typedef ::tensorflow::error::Code Code;
     28 
     29 // Append some context to an error message.  Each time we append
     30 // context put it on a new line, since it is possible for there
     31 // to be several layers of additional context.
     32 template <typename... Args>
     33 void AppendToMessage(::tensorflow::Status* status, Args... args) {
     34   *status = ::tensorflow::Status(
     35       status->code(),
     36       ::tensorflow::strings::StrCat(status->error_message(), "\n\t", args...));
     37 }
     38 
     39 // For propagating errors when calling a function.
     40 #define TF_RETURN_IF_ERROR(...)                          \
     41   do {                                                   \
     42     const ::tensorflow::Status _status = (__VA_ARGS__);  \
     43     if (TF_PREDICT_FALSE(!_status.ok())) return _status; \
     44   } while (0)
     45 
     46 #define TF_RETURN_WITH_CONTEXT_IF_ERROR(expr, ...)                  \
     47   do {                                                              \
     48     ::tensorflow::Status _status = (expr);                          \
     49     if (TF_PREDICT_FALSE(!_status.ok())) {                          \
     50       ::tensorflow::errors::AppendToMessage(&_status, __VA_ARGS__); \
     51       return _status;                                               \
     52     }                                                               \
     53   } while (0)
     54 
     55 // Convenience functions for generating and using error status.
     56 // Example usage:
     57 //   status.Update(errors::InvalidArgument("The ", foo, " isn't right."));
     58 //   if (errors::IsInvalidArgument(status)) { ... }
     59 //   switch (status.code()) { case error::INVALID_ARGUMENT: ... }
     60 
     61 #define DECLARE_ERROR(FUNC, CONST)                                       \
     62   template <typename... Args>                                            \
     63   ::tensorflow::Status FUNC(Args... args) {                              \
     64     return ::tensorflow::Status(::tensorflow::error::CONST,              \
     65                                 ::tensorflow::strings::StrCat(args...)); \
     66   }                                                                      \
     67   inline bool Is##FUNC(const ::tensorflow::Status& status) {             \
     68     return status.code() == ::tensorflow::error::CONST;                  \
     69   }
     70 
     71 DECLARE_ERROR(Cancelled, CANCELLED)
     72 DECLARE_ERROR(InvalidArgument, INVALID_ARGUMENT)
     73 DECLARE_ERROR(NotFound, NOT_FOUND)
     74 DECLARE_ERROR(AlreadyExists, ALREADY_EXISTS)
     75 DECLARE_ERROR(ResourceExhausted, RESOURCE_EXHAUSTED)
     76 DECLARE_ERROR(Unavailable, UNAVAILABLE)
     77 DECLARE_ERROR(FailedPrecondition, FAILED_PRECONDITION)
     78 DECLARE_ERROR(OutOfRange, OUT_OF_RANGE)
     79 DECLARE_ERROR(Unimplemented, UNIMPLEMENTED)
     80 DECLARE_ERROR(Internal, INTERNAL)
     81 DECLARE_ERROR(Aborted, ABORTED)
     82 DECLARE_ERROR(DeadlineExceeded, DEADLINE_EXCEEDED)
     83 DECLARE_ERROR(DataLoss, DATA_LOSS)
     84 DECLARE_ERROR(Unknown, UNKNOWN)
     85 DECLARE_ERROR(PermissionDenied, PERMISSION_DENIED)
     86 DECLARE_ERROR(Unauthenticated, UNAUTHENTICATED)
     87 
     88 #undef DECLARE_ERROR
     89 
     90 // The CanonicalCode() for non-errors.
     91 using ::tensorflow::error::OK;
     92 
     93 }  // namespace errors
     94 }  // namespace tensorflow
     95 
     96 #endif  // TENSORFLOW_LIB_CORE_ERRORS_H_
     97