1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/tsl/platform/errors.h"
17
18#include <errno.h>
19#include <string.h>
20
21#include "tensorflow/tsl/platform/status.h"
22#include "tensorflow/tsl/platform/strcat.h"
23
24namespace tsl {
25namespace errors {
26
27namespace {
28
29error::Code ErrnoToCode(int err_number) {
30 error::Code code;
31 switch (err_number) {
32 case 0:
33 code = error::Code::OK;
34 break;
35 case EINVAL: // Invalid argument
36 case ENAMETOOLONG: // Filename too long
37 case E2BIG: // Argument list too long
38 case EDESTADDRREQ: // Destination address required
39 case EDOM: // Mathematics argument out of domain of function
40 case EFAULT: // Bad address
41 case EILSEQ: // Illegal byte sequence
42 case ENOPROTOOPT: // Protocol not available
43 case ENOSTR: // Not a STREAM
44 case ENOTSOCK: // Not a socket
45 case ENOTTY: // Inappropriate I/O control operation
46 case EPROTOTYPE: // Protocol wrong type for socket
47 case ESPIPE: // Invalid seek
48 code = error::INVALID_ARGUMENT;
49 break;
50 case ETIMEDOUT: // Connection timed out
51 case ETIME: // Timer expired
52 code = error::DEADLINE_EXCEEDED;
53 break;
54 case ENODEV: // No such device
55 case ENOENT: // No such file or directory
56 case ENXIO: // No such device or address
57 case ESRCH: // No such process
58 code = error::NOT_FOUND;
59 break;
60 case EEXIST: // File exists
61 case EADDRNOTAVAIL: // Address not available
62 case EALREADY: // Connection already in progress
63 code = error::ALREADY_EXISTS;
64 break;
65 case EPERM: // Operation not permitted
66 case EACCES: // Permission denied
67 case EROFS: // Read only file system
68 code = error::PERMISSION_DENIED;
69 break;
70 case ENOTEMPTY: // Directory not empty
71 case EISDIR: // Is a directory
72 case ENOTDIR: // Not a directory
73 case EADDRINUSE: // Address already in use
74 case EBADF: // Invalid file descriptor
75 case EBUSY: // Device or resource busy
76 case ECHILD: // No child processes
77 case EISCONN: // Socket is connected
78#if !defined(_WIN32) && !defined(__HAIKU__)
79 case ENOTBLK: // Block device required
80#endif
81 case ENOTCONN: // The socket is not connected
82 case EPIPE: // Broken pipe
83#if !defined(_WIN32)
84 case ESHUTDOWN: // Cannot send after transport endpoint shutdown
85#endif
86 case ETXTBSY: // Text file busy
87 code = error::FAILED_PRECONDITION;
88 break;
89 case ENOSPC: // No space left on device
90#if !defined(_WIN32)
91 case EDQUOT: // Disk quota exceeded
92#endif
93 case EMFILE: // Too many open files
94 case EMLINK: // Too many links
95 case ENFILE: // Too many open files in system
96 case ENOBUFS: // No buffer space available
97 case ENODATA: // No message is available on the STREAM read queue
98 case ENOMEM: // Not enough space
99 case ENOSR: // No STREAM resources
100#if !defined(_WIN32) && !defined(__HAIKU__)
101 case EUSERS: // Too many users
102#endif
103 code = error::RESOURCE_EXHAUSTED;
104 break;
105 case EFBIG: // File too large
106 case EOVERFLOW: // Value too large to be stored in data type
107 case ERANGE: // Result too large
108 code = error::OUT_OF_RANGE;
109 break;
110 case ENOSYS: // Function not implemented
111 case ENOTSUP: // Operation not supported
112 case EAFNOSUPPORT: // Address family not supported
113#if !defined(_WIN32)
114 case EPFNOSUPPORT: // Protocol family not supported
115#endif
116 case EPROTONOSUPPORT: // Protocol not supported
117#if !defined(_WIN32) && !defined(__HAIKU__)
118 case ESOCKTNOSUPPORT: // Socket type not supported
119#endif
120 case EXDEV: // Improper link
121 code = error::UNIMPLEMENTED;
122 break;
123 case EAGAIN: // Resource temporarily unavailable
124 case ECONNREFUSED: // Connection refused
125 case ECONNABORTED: // Connection aborted
126 case ECONNRESET: // Connection reset
127 case EINTR: // Interrupted function call
128#if !defined(_WIN32)
129 case EHOSTDOWN: // Host is down
130#endif
131 case EHOSTUNREACH: // Host is unreachable
132 case ENETDOWN: // Network is down
133 case ENETRESET: // Connection aborted by network
134 case ENETUNREACH: // Network unreachable
135 case ENOLCK: // No locks available
136 case ENOLINK: // Link has been severed
137#if !(defined(__APPLE__) || defined(__FreeBSD__) || defined(_WIN32) || \
138 defined(__HAIKU__))
139 case ENONET: // Machine is not on the network
140#endif
141 code = error::UNAVAILABLE;
142 break;
143 case EDEADLK: // Resource deadlock avoided
144#if !defined(_WIN32)
145 case ESTALE: // Stale file handle
146#endif
147 code = error::ABORTED;
148 break;
149 case ECANCELED: // Operation cancelled
150 code = error::CANCELLED;
151 break;
152 // NOTE: If you get any of the following (especially in a
153 // reproducible way) and can propose a better mapping,
154 // please email the owners about updating this mapping.
155 case EBADMSG: // Bad message
156 case EIDRM: // Identifier removed
157 case EINPROGRESS: // Operation in progress
158 case EIO: // I/O error
159 case ELOOP: // Too many levels of symbolic links
160 case ENOEXEC: // Exec format error
161 case ENOMSG: // No message of the desired type
162 case EPROTO: // Protocol error
163#if !defined(_WIN32) && !defined(__HAIKU__)
164 case EREMOTE: // Object is remote
165#endif
166 code = error::UNKNOWN;
167 break;
168 default: {
169 code = error::UNKNOWN;
170 break;
171 }
172 }
173 return code;
174}
175
176} // namespace
177
178Status IOError(const string& context, int err_number) {
179 auto code = ErrnoToCode(err_number);
180 return Status(code, strings::StrCat(context, "; ", strerror(err_number)));
181}
182
183bool IsAborted(const Status& status) {
184 return status.code() == tsl::error::Code::ABORTED;
185}
186
187bool IsAlreadyExists(const Status& status) {
188 return status.code() == tsl::error::Code::ALREADY_EXISTS;
189}
190
191bool IsCancelled(const Status& status) {
192 return status.code() == tsl::error::Code::CANCELLED;
193}
194
195bool IsDataLoss(const Status& status) {
196 return status.code() == tsl::error::Code::DATA_LOSS;
197}
198
199bool IsDeadlineExceeded(const Status& status) {
200 return status.code() == tsl::error::Code::DEADLINE_EXCEEDED;
201}
202
203bool IsFailedPrecondition(const Status& status) {
204 return status.code() == tsl::error::Code::FAILED_PRECONDITION;
205}
206
207bool IsInternal(const Status& status) {
208 return status.code() == tsl::error::Code::INTERNAL;
209}
210
211bool IsInvalidArgument(const Status& status) {
212 return status.code() == tsl::error::Code::INVALID_ARGUMENT;
213}
214
215bool IsNotFound(const Status& status) {
216 return status.code() == tsl::error::Code::NOT_FOUND;
217}
218
219bool IsOutOfRange(const Status& status) {
220 return status.code() == tsl::error::Code::OUT_OF_RANGE;
221}
222
223bool IsPermissionDenied(const Status& status) {
224 return status.code() == tsl::error::Code::PERMISSION_DENIED;
225}
226
227bool IsResourceExhausted(const Status& status) {
228 return status.code() == tsl::error::Code::RESOURCE_EXHAUSTED;
229}
230
231bool IsUnauthenticated(const Status& status) {
232 return status.code() == tsl::error::Code::UNAUTHENTICATED;
233}
234
235bool IsUnavailable(const Status& status) {
236 return status.code() == tsl::error::Code::UNAVAILABLE;
237}
238
239bool IsUnimplemented(const Status& status) {
240 return status.code() == tsl::error::Code::UNIMPLEMENTED;
241}
242
243bool IsUnknown(const Status& status) {
244 return status.code() == tsl::error::Code::UNKNOWN;
245}
246
247} // namespace errors
248} // namespace tsl
249