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_CORE_PLATFORM_ERRORS_H_
17 #define TENSORFLOW_CORE_PLATFORM_ERRORS_H_
18
19 #include <sstream>
20
21 #include "absl/strings/str_join.h"
22 #include "tensorflow/core/platform/status.h"
23 #include "tensorflow/core/platform/logging.h"
24 #include "tensorflow/core/platform/macros.h"
25 #include "tensorflow/core/platform/str_util.h"
26 #include "tensorflow/core/platform/strcat.h"
27
28 namespace tensorflow {
29 namespace errors {
30
31 typedef ::tensorflow::error::Code Code;
32
33 namespace internal {
34
35 // The DECLARE_ERROR macro below only supports types that can be converted
36 // into StrCat's AlphaNum. For the other types we rely on a slower path
37 // through std::stringstream. To add support of a new type, it is enough to
38 // make sure there is an operator<<() for it:
39 //
40 // std::ostream& operator<<(std::ostream& os, const MyType& foo) {
41 // os << foo.ToString();
42 // return os;
43 // }
44 // Eventually absl::strings will have native support for this and we will be
45 // able to completely remove PrepareForStrCat().
46 template <typename T>
47 typename std::enable_if<!std::is_convertible<T, strings::AlphaNum>::value,
48 std::string>::type
PrepareForStrCat(const T & t)49 PrepareForStrCat(const T& t) {
50 std::stringstream ss;
51 ss << t;
52 return ss.str();
53 }
PrepareForStrCat(const strings::AlphaNum & a)54 inline const strings::AlphaNum& PrepareForStrCat(const strings::AlphaNum& a) {
55 return a;
56 }
57
58 } // namespace internal
59
60 // Append some context to an error message. Each time we append
61 // context put it on a new line, since it is possible for there
62 // to be several layers of additional context.
63 template <typename... Args>
AppendToMessage(::tensorflow::Status * status,Args...args)64 void AppendToMessage(::tensorflow::Status* status, Args... args) {
65 std::vector<StackFrame> stack_trace = status->stack_trace();
66 *status = ::tensorflow::Status(
67 status->code(),
68 ::tensorflow::strings::StrCat(status->error_message(), "\n\t", args...),
69 std::move(stack_trace));
70 }
71
72 // For propagating errors when calling a function.
73 #define TF_RETURN_IF_ERROR(...) \
74 do { \
75 ::tensorflow::Status _status = (__VA_ARGS__); \
76 if (TF_PREDICT_FALSE(!_status.ok())) return _status; \
77 } while (0)
78
79 #define TF_RETURN_WITH_CONTEXT_IF_ERROR(expr, ...) \
80 do { \
81 ::tensorflow::Status _status = (expr); \
82 if (TF_PREDICT_FALSE(!_status.ok())) { \
83 ::tensorflow::errors::AppendToMessage(&_status, __VA_ARGS__); \
84 return _status; \
85 } \
86 } while (0)
87
88 // Convenience functions for generating and using error status.
89 // Example usage:
90 // status.Update(errors::InvalidArgument("The ", foo, " isn't right."));
91 // if (errors::IsInvalidArgument(status)) { ... }
92 // switch (status.code()) { case error::INVALID_ARGUMENT: ... }
93
94 #define DECLARE_ERROR(FUNC, CONST) \
95 template <typename... Args> \
96 ::tensorflow::Status FUNC(Args... args) { \
97 return ::tensorflow::Status( \
98 ::tensorflow::error::CONST, \
99 ::tensorflow::strings::StrCat( \
100 ::tensorflow::errors::internal::PrepareForStrCat(args)...)); \
101 } \
102 inline bool Is##FUNC(const ::tensorflow::Status& status) { \
103 return status.code() == ::tensorflow::error::CONST; \
104 }
105
DECLARE_ERROR(Cancelled,CANCELLED)106 DECLARE_ERROR(Cancelled, CANCELLED)
107 DECLARE_ERROR(InvalidArgument, INVALID_ARGUMENT)
108 DECLARE_ERROR(NotFound, NOT_FOUND)
109 DECLARE_ERROR(AlreadyExists, ALREADY_EXISTS)
110 DECLARE_ERROR(ResourceExhausted, RESOURCE_EXHAUSTED)
111 DECLARE_ERROR(Unavailable, UNAVAILABLE)
112 DECLARE_ERROR(FailedPrecondition, FAILED_PRECONDITION)
113 DECLARE_ERROR(OutOfRange, OUT_OF_RANGE)
114 DECLARE_ERROR(Unimplemented, UNIMPLEMENTED)
115 DECLARE_ERROR(Internal, INTERNAL)
116 DECLARE_ERROR(Aborted, ABORTED)
117 DECLARE_ERROR(DeadlineExceeded, DEADLINE_EXCEEDED)
118 DECLARE_ERROR(DataLoss, DATA_LOSS)
119 DECLARE_ERROR(Unknown, UNKNOWN)
120 DECLARE_ERROR(PermissionDenied, PERMISSION_DENIED)
121 DECLARE_ERROR(Unauthenticated, UNAUTHENTICATED)
122
123 #undef DECLARE_ERROR
124
125 // Produces a formatted string pattern from the name which can uniquely identify
126 // this node upstream to produce an informative error message. The pattern
127 // followed is: {{node <name>}}
128 // Note: The pattern below determines the regex _NODEDEF_NAME_RE in the file
129 // tensorflow/python/client/session.py
130 // LINT.IfChange
131 inline std::string FormatNodeNameForError(const std::string& name) {
132 return strings::StrCat("{{node ", name, "}}");
133 }
134 // LINT.ThenChange(//tensorflow/python/client/session.py)
135 template <typename T>
FormatNodeNamesForError(const T & names)136 std::string FormatNodeNamesForError(const T& names) {
137 return absl::StrJoin(
138 names, ", ", [](std::string* output, const std::string& s) {
139 ::tensorflow::strings::StrAppend(output, FormatNodeNameForError(s));
140 });
141 }
142 // LINT.IfChange
FormatColocationNodeForError(const std::string & name)143 inline std::string FormatColocationNodeForError(const std::string& name) {
144 return strings::StrCat("{{colocation_node ", name, "}}");
145 }
146 // LINT.ThenChange(//tensorflow/python/framework/error_interpolation.py)
147 template <typename T>
FormatColocationNodeForError(const T & names)148 std::string FormatColocationNodeForError(const T& names) {
149 return absl::StrJoin(names, ", ",
150 [](std::string* output, const std::string& s) {
151 ::tensorflow::strings::StrAppend(
152 output, FormatColocationNodeForError(s));
153 });
154 }
155
FormatFunctionForError(const std::string & name)156 inline std::string FormatFunctionForError(const std::string& name) {
157 return strings::StrCat("{{function_node ", name, "}}");
158 }
159
160 // The CanonicalCode() for non-errors.
161 using ::tensorflow::error::OK;
162
163 } // namespace errors
164 } // namespace tensorflow
165
166 #endif // TENSORFLOW_CORE_PLATFORM_ERRORS_H_
167