1# Copyright 2015 gRPC authors. 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 15require_relative './grpc' 16 17# GRPC contains the General RPC module. 18module GRPC 19 # BadStatus is an exception class that indicates that an error occurred at 20 # either end of a GRPC connection. When raised, it indicates that a status 21 # error should be returned to the other end of a GRPC connection; when 22 # caught it means that this end received a status error. 23 # 24 # There is also subclass of BadStatus in this module for each GRPC status. 25 # E.g., the GRPC::Cancelled class corresponds to status CANCELLED. 26 # 27 # See 28 # https://github.com/grpc/grpc/blob/master/include/grpc/impl/codegen/status.h 29 # for detailed descriptions of each status code. 30 class BadStatus < StandardError 31 attr_reader :code, :details, :metadata 32 33 include GRPC::Core::StatusCodes 34 35 # @param code [Numeric] the status code 36 # @param details [String] the details of the exception 37 # @param metadata [Hash] the error's metadata 38 def initialize(code, details = 'unknown cause', metadata = {}) 39 super("#{code}:#{details}") 40 @code = code 41 @details = details 42 @metadata = metadata 43 end 44 45 # Converts the exception to a GRPC::Status for use in the networking 46 # wrapper layer. 47 # 48 # @return [Status] with the same code and details 49 def to_status 50 Struct::Status.new(code, details, @metadata) 51 end 52 53 def self.new_status_exception(code, details = 'unknown cause', 54 metadata = {}) 55 codes = {} 56 codes[OK] = Ok 57 codes[CANCELLED] = Cancelled 58 codes[UNKNOWN] = Unknown 59 codes[INVALID_ARGUMENT] = InvalidArgument 60 codes[DEADLINE_EXCEEDED] = DeadlineExceeded 61 codes[NOT_FOUND] = NotFound 62 codes[ALREADY_EXISTS] = AlreadyExists 63 codes[PERMISSION_DENIED] = PermissionDenied 64 codes[UNAUTHENTICATED] = Unauthenticated 65 codes[RESOURCE_EXHAUSTED] = ResourceExhausted 66 codes[FAILED_PRECONDITION] = FailedPrecondition 67 codes[ABORTED] = Aborted 68 codes[OUT_OF_RANGE] = OutOfRange 69 codes[UNIMPLEMENTED] = Unimplemented 70 codes[INTERNAL] = Internal 71 codes[UNIMPLEMENTED] = Unimplemented 72 codes[UNAVAILABLE] = Unavailable 73 codes[DATA_LOSS] = DataLoss 74 75 if codes[code].nil? 76 BadStatus.new(code, details, metadata) 77 else 78 codes[code].new(details, metadata) 79 end 80 end 81 end 82 83 # GRPC status code corresponding to status OK 84 class Ok < BadStatus 85 def initialize(details = 'unknown cause', metadata = {}) 86 super(Core::StatusCodes::OK, details, metadata) 87 end 88 end 89 90 # GRPC status code corresponding to status CANCELLED 91 class Cancelled < BadStatus 92 def initialize(details = 'unknown cause', metadata = {}) 93 super(Core::StatusCodes::CANCELLED, details, metadata) 94 end 95 end 96 97 # GRPC status code corresponding to status UNKNOWN 98 class Unknown < BadStatus 99 def initialize(details = 'unknown cause', metadata = {}) 100 super(Core::StatusCodes::UNKNOWN, details, metadata) 101 end 102 end 103 104 # GRPC status code corresponding to status INVALID_ARGUMENT 105 class InvalidArgument < BadStatus 106 def initialize(details = 'unknown cause', metadata = {}) 107 super(Core::StatusCodes::INVALID_ARGUMENT, details, metadata) 108 end 109 end 110 111 # GRPC status code corresponding to status DEADLINE_EXCEEDED 112 class DeadlineExceeded < BadStatus 113 def initialize(details = 'unknown cause', metadata = {}) 114 super(Core::StatusCodes::DEADLINE_EXCEEDED, details, metadata) 115 end 116 end 117 118 # GRPC status code corresponding to status NOT_FOUND 119 class NotFound < BadStatus 120 def initialize(details = 'unknown cause', metadata = {}) 121 super(Core::StatusCodes::NOT_FOUND, details, metadata) 122 end 123 end 124 125 # GRPC status code corresponding to status ALREADY_EXISTS 126 class AlreadyExists < BadStatus 127 def initialize(details = 'unknown cause', metadata = {}) 128 super(Core::StatusCodes::ALREADY_EXISTS, details, metadata) 129 end 130 end 131 132 # GRPC status code corresponding to status PERMISSION_DENIED 133 class PermissionDenied < BadStatus 134 def initialize(details = 'unknown cause', metadata = {}) 135 super(Core::StatusCodes::PERMISSION_DENIED, details, metadata) 136 end 137 end 138 139 # GRPC status code corresponding to status UNAUTHENTICATED 140 class Unauthenticated < BadStatus 141 def initialize(details = 'unknown cause', metadata = {}) 142 super(Core::StatusCodes::UNAUTHENTICATED, details, metadata) 143 end 144 end 145 146 # GRPC status code corresponding to status RESOURCE_EXHAUSTED 147 class ResourceExhausted < BadStatus 148 def initialize(details = 'unknown cause', metadata = {}) 149 super(Core::StatusCodes::RESOURCE_EXHAUSTED, details, metadata) 150 end 151 end 152 153 # GRPC status code corresponding to status FAILED_PRECONDITION 154 class FailedPrecondition < BadStatus 155 def initialize(details = 'unknown cause', metadata = {}) 156 super(Core::StatusCodes::FAILED_PRECONDITION, details, metadata) 157 end 158 end 159 160 # GRPC status code corresponding to status ABORTED 161 class Aborted < BadStatus 162 def initialize(details = 'unknown cause', metadata = {}) 163 super(Core::StatusCodes::ABORTED, details, metadata) 164 end 165 end 166 167 # GRPC status code corresponding to status OUT_OF_RANGE 168 class OutOfRange < BadStatus 169 def initialize(details = 'unknown cause', metadata = {}) 170 super(Core::StatusCodes::OUT_OF_RANGE, details, metadata) 171 end 172 end 173 174 # GRPC status code corresponding to status UNIMPLEMENTED 175 class Unimplemented < BadStatus 176 def initialize(details = 'unknown cause', metadata = {}) 177 super(Core::StatusCodes::UNIMPLEMENTED, details, metadata) 178 end 179 end 180 181 # GRPC status code corresponding to status INTERNAL 182 class Internal < BadStatus 183 def initialize(details = 'unknown cause', metadata = {}) 184 super(Core::StatusCodes::INTERNAL, details, metadata) 185 end 186 end 187 188 # GRPC status code corresponding to status UNAVAILABLE 189 class Unavailable < BadStatus 190 def initialize(details = 'unknown cause', metadata = {}) 191 super(Core::StatusCodes::UNAVAILABLE, details, metadata) 192 end 193 end 194 195 # GRPC status code corresponding to status DATA_LOSS 196 class DataLoss < BadStatus 197 def initialize(details = 'unknown cause', metadata = {}) 198 super(Core::StatusCodes::DATA_LOSS, details, metadata) 199 end 200 end 201end 202