gRPC.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2016, gRPC 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. #if SWIFT_PACKAGE
  17. import CgRPC
  18. #endif
  19. import Foundation // for String.Encoding
  20. public final class gRPC {
  21. private init() { } // Static members only.
  22. /// Initializes gRPC system
  23. public static func initialize() {
  24. grpc_init()
  25. }
  26. /// Shuts down gRPC system
  27. public static func shutdown() {
  28. grpc_shutdown()
  29. }
  30. /// Returns version of underlying gRPC library
  31. ///
  32. /// Returns: gRPC version string
  33. public static var version: String {
  34. // These two should always be valid UTF-8 strings, so we can forcibly unwrap them.
  35. return String(cString: grpc_version_string(), encoding: String.Encoding.utf8)!
  36. }
  37. /// Returns name associated with gRPC version
  38. ///
  39. /// Returns: gRPC version name
  40. public static var gStandsFor: String {
  41. return String(cString: grpc_g_stands_for(), encoding: String.Encoding.utf8)!
  42. }
  43. }
  44. /// Status codes for gRPC operations (replicated from status_code_enum.h)
  45. public enum StatusCode: Int {
  46. /// Not an error; returned on success.
  47. case ok = 0
  48. /// The operation was cancelled (typically by the caller).
  49. case cancelled = 1
  50. /// Unknown error. An example of where this error may be returned is if a
  51. /// Status value received from another address space belongs to an error-space
  52. /// that is not known in this address space. Also errors raised by APIs that
  53. /// do not return enough error information may be converted to this error.
  54. case unknown = 2
  55. /// Client specified an invalid argument. Note that this differs from
  56. /// FAILED_PRECONDITION. INVALID_ARGUMENT indicates arguments that are
  57. /// problematic regardless of the state of the system (e.g., a malformed file
  58. /// name).
  59. case invalidArgument = 3
  60. /// Deadline expired before operation could complete. For operations that
  61. /// change the state of the system, this error may be returned even if the
  62. /// operation has completed successfully. For example, a successful response
  63. /// from a server could have been delayed long enough for the deadline to
  64. /// expire.
  65. case deadlineExceeded = 4
  66. /// Some requested entity (e.g., file or directory) was not found.
  67. case notFound = 5
  68. /// Some entity that we attempted to create (e.g., file or directory) already
  69. /// exists.
  70. case alreadyExists = 6
  71. /// The caller does not have permission to execute the specified operation.
  72. /// PERMISSION_DENIED must not be used for rejections caused by exhausting
  73. /// some resource (use RESOURCE_EXHAUSTED instead for those errors).
  74. /// PERMISSION_DENIED must not be used if the caller can not be identified
  75. /// (use UNAUTHENTICATED instead for those errors).
  76. case permissionDenied = 7
  77. /// The request does not have valid authentication credentials for the
  78. /// operation.
  79. case unauthenticated = 16
  80. /// Some resource has been exhausted, perhaps a per-user quota, or perhaps the
  81. /// entire file system is out of space.
  82. case resourceExhausted = 8
  83. /// Operation was rejected because the system is not in a state required for
  84. /// the operation's execution. For example, directory to be deleted may be
  85. /// non-empty, an rmdir operation is applied to a non-directory, etc.
  86. ///
  87. /// A litmus test that may help a service implementor in deciding
  88. /// between FAILED_PRECONDITION, ABORTED, and UNAVAILABLE:
  89. /// (a) Use UNAVAILABLE if the client can retry just the failing call.
  90. /// (b) Use ABORTED if the client should retry at a higher-level
  91. /// (e.g., restarting a read-modify-write sequence).
  92. /// (c) Use FAILED_PRECONDITION if the client should not retry until
  93. /// the system state has been explicitly fixed. E.g., if an "rmdir"
  94. /// fails because the directory is non-empty, FAILED_PRECONDITION
  95. /// should be returned since the client should not retry unless
  96. /// they have first fixed up the directory by deleting files from it.
  97. /// (d) Use FAILED_PRECONDITION if the client performs conditional
  98. /// REST Get/Update/Delete on a resource and the resource on the
  99. /// server does not match the condition. E.g., conflicting
  100. /// read-modify-write on the same resource.
  101. case failedPrecondition = 9
  102. /// The operation was aborted, typically due to a concurrency issue like
  103. /// sequencer check failures, transaction aborts, etc.
  104. ///
  105. /// See litmus test above for deciding between FAILED_PRECONDITION, ABORTED,
  106. /// and UNAVAILABLE.
  107. case aborted = 10
  108. /// Operation was attempted past the valid range. E.g., seeking or reading
  109. /// past end of file.
  110. ///
  111. /// Unlike INVALID_ARGUMENT, this error indicates a problem that may be fixed
  112. /// if the system state changes. For example, a 32-bit file system will
  113. /// generate INVALID_ARGUMENT if asked to read at an offset that is not in the
  114. /// range [0,2^32-1], but it will generate OUT_OF_RANGE if asked to read from
  115. /// an offset past the current file size.
  116. ///
  117. /// There is a fair bit of overlap between FAILED_PRECONDITION and
  118. /// OUT_OF_RANGE. We recommend using OUT_OF_RANGE (the more specific error)
  119. /// when it applies so that callers who are iterating through a space can
  120. /// easily look for an OUT_OF_RANGE error to detect when they are done.
  121. case outOfRange = 11
  122. /// Operation is not implemented or not supported/enabled in this service.
  123. case unimplemented = 12
  124. /// Internal errors. Means some invariants expected by underlying System has
  125. /// been broken. If you see one of these errors, Something is very broken.
  126. case internalError = 13
  127. /// The service is currently unavailable. This is a most likely a transient
  128. /// condition and may be corrected by retrying with a backoff.
  129. ///
  130. /// See litmus test above for deciding between FAILED_PRECONDITION, ABORTED,
  131. /// and UNAVAILABLE.
  132. case unavailable = 14
  133. /// Unrecoverable data loss or corruption.
  134. case dataLoss = 15
  135. /// Force users to include a default branch:
  136. case doNotUse = -1
  137. }