github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/codes/codes.go (about)

     1  /*
     2   *
     3   * Copyright 2014 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   * This file may have been modified by CloudWeGo authors. All CloudWeGo
    18   * Modifications are Copyright 2021 CloudWeGo Authors.
    19   */
    20  
    21  // Package codes defines the canonical error codes used by gRPC. It is
    22  // consistent across various languages.
    23  package codes
    24  
    25  import (
    26  	"fmt"
    27  	"strconv"
    28  )
    29  
    30  // A Code is an unsigned 32-bit error code as defined in the gRPC spec.
    31  type Code uint32
    32  
    33  const (
    34  	// OK is returned on success.
    35  	OK Code = 0
    36  
    37  	// Canceled indicates the operation was canceled (typically by the caller).
    38  	Canceled Code = 1
    39  
    40  	// Unknown error. An example of where this error may be returned is
    41  	// if a Status value received from another address space belongs to
    42  	// an error-space that is not known in this address space. Also
    43  	// errors raised by APIs that do not return enough error information
    44  	// may be converted to this error.
    45  	Unknown Code = 2
    46  
    47  	// InvalidArgument indicates client specified an invalid argument.
    48  	// Note that this differs from FailedPrecondition. It indicates arguments
    49  	// that are problematic regardless of the state of the system
    50  	// (e.g., a malformed file name).
    51  	InvalidArgument Code = 3
    52  
    53  	// DeadlineExceeded means operation expired before completion.
    54  	// For operations that change the state of the system, this error may be
    55  	// returned even if the operation has completed successfully. For
    56  	// example, a successful response from a server could have been delayed
    57  	// long enough for the deadline to expire.
    58  	DeadlineExceeded Code = 4
    59  
    60  	// NotFound means some requested entity (e.g., file or directory) was
    61  	// not found.
    62  	NotFound Code = 5
    63  
    64  	// AlreadyExists means an attempt to create an entity failed because one
    65  	// already exists.
    66  	AlreadyExists Code = 6
    67  
    68  	// PermissionDenied indicates the caller does not have permission to
    69  	// execute the specified operation. It must not be used for rejections
    70  	// caused by exhausting some resource (use ResourceExhausted
    71  	// instead for those errors). It must not be
    72  	// used if the caller cannot be identified (use Unauthenticated
    73  	// instead for those errors).
    74  	PermissionDenied Code = 7
    75  
    76  	// ResourceExhausted indicates some resource has been exhausted, perhaps
    77  	// a per-user quota, or perhaps the entire file system is out of space.
    78  	ResourceExhausted Code = 8
    79  
    80  	// FailedPrecondition indicates operation was rejected because the
    81  	// system is not in a state required for the operation's execution.
    82  	// For example, directory to be deleted may be non-empty, an rmdir
    83  	// operation is applied to a non-directory, etc.
    84  	//
    85  	// A litmus test that may help a service implementor in deciding
    86  	// between FailedPrecondition, Aborted, and Unavailable:
    87  	//  (a) Use Unavailable if the client can retry just the failing call.
    88  	//  (b) Use Aborted if the client should retry at a higher-level
    89  	//      (e.g., restarting a read-modify-write sequence).
    90  	//  (c) Use FailedPrecondition if the client should not retry until
    91  	//      the system state has been explicitly fixed. E.g., if an "rmdir"
    92  	//      fails because the directory is non-empty, FailedPrecondition
    93  	//      should be returned since the client should not retry unless
    94  	//      they have first fixed up the directory by deleting files from it.
    95  	//  (d) Use FailedPrecondition if the client performs conditional
    96  	//      REST Get/Update/Delete on a resource and the resource on the
    97  	//      server does not match the condition. E.g., conflicting
    98  	//      read-modify-write on the same resource.
    99  	FailedPrecondition Code = 9
   100  
   101  	// Aborted indicates the operation was aborted, typically due to a
   102  	// concurrency issue like sequencer check failures, transaction aborts,
   103  	// etc.
   104  	//
   105  	// See litmus test above for deciding between FailedPrecondition,
   106  	// Aborted, and Unavailable.
   107  	Aborted Code = 10
   108  
   109  	// OutOfRange means operation was attempted past the valid range.
   110  	// E.g., seeking or reading past end of file.
   111  	//
   112  	// Unlike InvalidArgument, this error indicates a problem that may
   113  	// be fixed if the system state changes. For example, a 32-bit file
   114  	// system will generate InvalidArgument if asked to read at an
   115  	// offset that is not in the range [0,2^32-1], but it will generate
   116  	// OutOfRange if asked to read from an offset past the current
   117  	// file size.
   118  	//
   119  	// There is a fair bit of overlap between FailedPrecondition and
   120  	// OutOfRange. We recommend using OutOfRange (the more specific
   121  	// error) when it applies so that callers who are iterating through
   122  	// a space can easily look for an OutOfRange error to detect when
   123  	// they are done.
   124  	OutOfRange Code = 11
   125  
   126  	// Unimplemented indicates operation is not implemented or not
   127  	// supported/enabled in this service.
   128  	Unimplemented Code = 12
   129  
   130  	// Internal errors. Means some invariants expected by underlying
   131  	// system has been broken. If you see one of these errors,
   132  	// something is very broken.
   133  	Internal Code = 13
   134  
   135  	// Unavailable indicates the service is currently unavailable.
   136  	// This is a most likely a transient condition and may be corrected
   137  	// by retrying with a backoff. Note that it is not always safe to retry
   138  	// non-idempotent operations.
   139  	//
   140  	// See litmus test above for deciding between FailedPrecondition,
   141  	// Aborted, and Unavailable.
   142  	Unavailable Code = 14
   143  
   144  	// DataLoss indicates unrecoverable data loss or corruption.
   145  	DataLoss Code = 15
   146  
   147  	// Unauthenticated indicates the request does not have valid
   148  	// authentication credentials for the operation.
   149  	Unauthenticated Code = 16
   150  
   151  	_maxCode = 17
   152  )
   153  
   154  var strToCode = map[string]Code{
   155  	`"OK"`: OK,
   156  	`"CANCELLED"`:/* [sic] */ Canceled,
   157  	`"UNKNOWN"`:             Unknown,
   158  	`"INVALID_ARGUMENT"`:    InvalidArgument,
   159  	`"DEADLINE_EXCEEDED"`:   DeadlineExceeded,
   160  	`"NOT_FOUND"`:           NotFound,
   161  	`"ALREADY_EXISTS"`:      AlreadyExists,
   162  	`"PERMISSION_DENIED"`:   PermissionDenied,
   163  	`"RESOURCE_EXHAUSTED"`:  ResourceExhausted,
   164  	`"FAILED_PRECONDITION"`: FailedPrecondition,
   165  	`"ABORTED"`:             Aborted,
   166  	`"OUT_OF_RANGE"`:        OutOfRange,
   167  	`"UNIMPLEMENTED"`:       Unimplemented,
   168  	`"INTERNAL"`:            Internal,
   169  	`"UNAVAILABLE"`:         Unavailable,
   170  	`"DATA_LOSS"`:           DataLoss,
   171  	`"UNAUTHENTICATED"`:     Unauthenticated,
   172  }
   173  
   174  // UnmarshalJSON unmarshals b into the Code.
   175  func (c *Code) UnmarshalJSON(b []byte) error {
   176  	// From json.Unmarshaler: By convention, to approximate the behavior of
   177  	// Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as
   178  	// a no-op.
   179  	if string(b) == "null" {
   180  		return nil
   181  	}
   182  	if c == nil {
   183  		return fmt.Errorf("nil receiver passed to UnmarshalJSON")
   184  	}
   185  
   186  	if ci, err := strconv.ParseUint(string(b), 10, 32); err == nil {
   187  		if ci >= _maxCode {
   188  			return fmt.Errorf("invalid code: %q", ci)
   189  		}
   190  
   191  		*c = Code(ci)
   192  		return nil
   193  	}
   194  
   195  	if jc, ok := strToCode[string(b)]; ok {
   196  		*c = jc
   197  		return nil
   198  	}
   199  	return fmt.Errorf("invalid code: %q", string(b))
   200  }