github.com/cloudwego/kitex@v0.9.0/pkg/remote/codec/perrors/protocol_error.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     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  package perrors
    18  
    19  import "errors"
    20  
    21  // ProtocolError indicates an protocol error has occurred.
    22  type ProtocolError interface {
    23  	error
    24  	TypeId() int
    25  }
    26  
    27  // 0-6 corresponding with thrift protocol_exception
    28  const (
    29  	UnknownProtocolError = 0 // unknown error
    30  	InvalidData          = 1
    31  	NegativeSize         = 2
    32  	SizeLimit            = 3
    33  	BadVersion           = 4
    34  	NotImplemented       = 5
    35  	DepthLimit           = 6
    36  )
    37  
    38  // InvalidDataLength indicates the data length is invalid.
    39  var InvalidDataLength = NewProtocolErrorWithType(InvalidData, "Invalid data length")
    40  
    41  type protocolException struct {
    42  	typeID  int
    43  	message string
    44  	rawErr  error
    45  }
    46  
    47  // TypeId implements the ProtocolError interface.
    48  func (p *protocolException) TypeId() int {
    49  	return p.typeID
    50  }
    51  
    52  // String implements the ProtocolError interface.
    53  func (p *protocolException) String() string {
    54  	return p.message
    55  }
    56  
    57  // Error implements the ProtocolError interface.
    58  func (p *protocolException) Error() string {
    59  	return p.message
    60  }
    61  
    62  // Unwrap enables protocolException to use methods in errors lib.
    63  func (p *protocolException) Unwrap() error {
    64  	return p.rawErr
    65  }
    66  
    67  // Is enables protocolException to use methods in errors lib.
    68  func (p *protocolException) Is(target error) bool {
    69  	return p == target || errors.Is(p.rawErr, target)
    70  }
    71  
    72  // NewProtocolError creates a new ProtocolError wrapping the given error.
    73  func NewProtocolError(err error) error {
    74  	if err == nil {
    75  		return nil
    76  	}
    77  	if e, ok := err.(ProtocolError); ok {
    78  		return e
    79  	}
    80  	return &protocolException{typeID: UnknownProtocolError, message: err.Error(), rawErr: err}
    81  }
    82  
    83  // NewProtocolErrorWithErrMsg to build protocolException with rawErr and errMsg
    84  func NewProtocolErrorWithErrMsg(err error, errMsg string) error {
    85  	if err == nil {
    86  		return nil
    87  	}
    88  	if e, ok := err.(ProtocolError); ok {
    89  		return e
    90  	}
    91  	return &protocolException{typeID: UnknownProtocolError, message: errMsg, rawErr: err}
    92  }
    93  
    94  // NewProtocolErrorWithMsg to build protocolException with errMsg
    95  func NewProtocolErrorWithMsg(errMsg string) error {
    96  	return &protocolException{typeID: UnknownProtocolError, message: errMsg}
    97  }
    98  
    99  // NewProtocolErrorWithType to build protocolException with errType and errMsg
   100  func NewProtocolErrorWithType(errType int, errMsg string) ProtocolError {
   101  	return &protocolException{typeID: errType, message: errMsg}
   102  }
   103  
   104  // IsProtocolError to assert if the err is ProtocolError which has TypeId() func
   105  func IsProtocolError(err error) bool {
   106  	_, ok := err.(ProtocolError)
   107  	return ok
   108  }