kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/status.cc (about)

     1  /*
     2   * Copyright 2018 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  #include "kythe/cxx/common/status.h"
    18  
    19  #include <errno.h>
    20  
    21  #include <cstring>
    22  
    23  #include "absl/status/status.h"
    24  
    25  namespace kythe {
    26  
    27  absl::StatusCode ErrnoToStatusCode(int error_number) {
    28    using absl::StatusCode;
    29    switch (error_number) {
    30      case 0:
    31        return StatusCode::kOk;
    32      case EINVAL:        // Invalid argument
    33      case ENAMETOOLONG:  // Filename too long
    34      case E2BIG:         // Argument list too long
    35      case EDESTADDRREQ:  // Destination address required
    36      case EDOM:          // Mathematics argument out of domain of function
    37      case EFAULT:        // Bad address
    38      case EILSEQ:        // Illegal byte sequence
    39      case ENOPROTOOPT:   // Protocol not available
    40      case ENOSTR:        // Not a STREAM
    41      case ENOTSOCK:      // Not a socket
    42      case ENOTTY:        // Inappropriate I/O control operation
    43      case EPROTOTYPE:    // Protocol wrong type for socket
    44      case ESPIPE:        // Invalid seek
    45        return StatusCode::kInvalidArgument;
    46      case ETIMEDOUT:  // Connection timed out
    47      case ETIME:      // Timer expired
    48        return StatusCode::kDeadlineExceeded;
    49      case ENODEV:  // No such device
    50      case ENOENT:  // No such file or directory
    51      case ENXIO:   // No such device or address
    52      case ESRCH:   // No such process
    53        return StatusCode::kNotFound;
    54      case EEXIST:         // File exists
    55      case EADDRNOTAVAIL:  // Address not available
    56      case EALREADY:       // Connection already in progress
    57        return StatusCode::kAlreadyExists;
    58      case EPERM:   // Operation not permitted
    59      case EACCES:  // Permission denied
    60      case EROFS:   // Read only file system
    61        return StatusCode::kPermissionDenied;
    62      case ENOTEMPTY:   // Directory not empty
    63      case EISDIR:      // Is a directory
    64      case ENOTDIR:     // Not a directory
    65      case EADDRINUSE:  // Address already in use
    66      case EBADF:       // Invalid file descriptor
    67      case EBUSY:       // Device or resource busy
    68      case ECHILD:      // No child processes
    69      case EISCONN:     // Socket is connected
    70      case ENOTBLK:     // Block device required
    71      case ENOTCONN:    // The socket is not connected
    72      case EPIPE:       // Broken pipe
    73      case ESHUTDOWN:   // Cannot send after transport endpoint shutdown
    74      case ETXTBSY:     // Text file busy
    75        return StatusCode::kFailedPrecondition;
    76      case ENOSPC:   // No space left on device
    77      case EDQUOT:   // Disk quota exceeded
    78      case EMFILE:   // Too many open files
    79      case EMLINK:   // Too many links
    80      case ENFILE:   // Too many open files in system
    81      case ENOBUFS:  // No buffer space available
    82      case ENODATA:  // No message is available on the STREAM read queue
    83      case ENOMEM:   // Not enough space
    84      case ENOSR:    // No STREAM resources
    85      case EUSERS:   // Too many users
    86        return StatusCode::kResourceExhausted;
    87      case EFBIG:      // File too large
    88      case EOVERFLOW:  // Value too large to be stored in data type
    89      case ERANGE:     // Result too large
    90        return StatusCode::kOutOfRange;
    91      case ENOSYS:           // Function not implemented
    92      case ENOTSUP:          // Operation not supported
    93      case EAFNOSUPPORT:     // Address family not supported
    94      case EPFNOSUPPORT:     // Protocol family not supported
    95      case EPROTONOSUPPORT:  // Protocol not supported
    96      case ESOCKTNOSUPPORT:  // Socket type not supported
    97      case EXDEV:            // Improper link
    98        return StatusCode::kUnimplemented;
    99      case EAGAIN:        // Resource temporarily unavailable
   100      case ECONNREFUSED:  // Connection refused
   101      case ECONNABORTED:  // Connection aborted
   102      case ECONNRESET:    // Connection reset
   103      case EINTR:         // Interrupted function call
   104      case EHOSTDOWN:     // Host is down
   105      case EHOSTUNREACH:  // Host is unreachable
   106      case ENETDOWN:      // Network is down
   107      case ENETRESET:     // Connection aborted by network
   108      case ENETUNREACH:   // Network unreachable
   109      case ENOLCK:        // No locks available
   110      case ENOLINK:       // Link has been severed
   111        return StatusCode::kUnavailable;
   112      case EDEADLK:  // Resource deadlock avoided
   113      case ESTALE:   // Stale file handle
   114        return StatusCode::kAborted;
   115      case ECANCELED:  // Operation cancelled
   116        return StatusCode::kCancelled;
   117      default:
   118        return StatusCode::kUnknown;
   119    }
   120  }
   121  
   122  absl::StatusCode ErrnoToStatusCode() { return ErrnoToStatusCode(errno); }
   123  
   124  absl::Status ErrnoToStatus(int error_number) {
   125    return absl::Status(ErrnoToStatusCode(error_number), strerror(error_number));
   126  }
   127  
   128  absl::Status ErrnoToStatus() { return ErrnoToStatus(errno); }
   129  
   130  }  // namespace kythe