File: error_codes.rb

package info (click to toggle)
ruby-gapic-common 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 392 kB
  • sloc: ruby: 2,081; makefile: 4
file content (74 lines) | stat: -rw-r--r-- 2,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Gapic
  module Common
    ##
    # @private
    # The gRPC error codes and their HTTP mapping
    #
    module ErrorCodes
      # @private
      # See https://grpc.github.io/grpc/core/md_doc_statuscodes.html for a
      # list of error codes.
      error_code_mapping = [
        "OK",
        "CANCELLED",
        "UNKNOWN",
        "INVALID_ARGUMENT",
        "DEADLINE_EXCEEDED",
        "NOT_FOUND",
        "ALREADY_EXISTS",
        "PERMISSION_DENIED",
        "RESOURCE_EXHAUSTED",
        "FAILED_PRECONDITION",
        "ABORTED",
        "OUT_OF_RANGE",
        "UNIMPLEMENTED",
        "INTERNAL",
        "UNAVAILABLE",
        "DATA_LOSS",
        "UNAUTHENTICATED"
      ].freeze

      # @private
      ERROR_STRING_MAPPING = error_code_mapping.each_with_index.to_h.freeze

      # @private
      HTTP_GRPC_CODE_MAP = {
        400 => 3, # InvalidArgumentError
        401 => 16, # UnauthenticatedError
        403 => 7, # PermissionDeniedError
        404 => 5, # NotFoundError
        409 => 6, # AlreadyExistsError
        412 => 9, # FailedPreconditionError
        429 => 8, # ResourceExhaustedError
        499 => 1, # CanceledError
        500 => 13, # InternalError
        501 => 12, # UnimplementedError
        503 => 14, # UnavailableError
        504 => 4 # DeadlineExceededError
      }.freeze

      # @private
      # Converts http error codes into corresponding gRPC ones
      def self.grpc_error_for http_error_code
        return 2 unless http_error_code

        # The http status codes mapped to their error classes.
        HTTP_GRPC_CODE_MAP[http_error_code] || 2 # UnknownError
      end
    end
  end
end