github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/xdsclient/xdsresource/errors.go (about)

     1  /*
     2   *
     3   * Copyright 2020 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   */
    18  
    19  package xdsresource
    20  
    21  import "fmt"
    22  
    23  // ErrorType is the type of the error that the watcher will receive from the xds
    24  // client.
    25  type ErrorType int
    26  
    27  const (
    28  	// ErrorTypeUnknown indicates the error doesn't have a specific type. It is
    29  	// the default value, and is returned if the error is not an xds error.
    30  	ErrorTypeUnknown ErrorType = iota
    31  	// ErrorTypeConnection indicates a connection error from the gRPC client.
    32  	ErrorTypeConnection
    33  	// ErrorTypeResourceNotFound indicates a resource is not found from the xds
    34  	// response. It's typically returned if the resource is removed in the xds
    35  	// server.
    36  	ErrorTypeResourceNotFound
    37  )
    38  
    39  type xdsClientError struct {
    40  	t    ErrorType
    41  	desc string
    42  }
    43  
    44  func (e *xdsClientError) Error() string {
    45  	return e.desc
    46  }
    47  
    48  // NewErrorf creates an xds client error. The callbacks are called with this
    49  // error, to pass additional information about the error.
    50  func NewErrorf(t ErrorType, format string, args ...interface{}) error {
    51  	return &xdsClientError{t: t, desc: fmt.Sprintf(format, args...)}
    52  }
    53  
    54  // ErrType returns the error's type.
    55  func ErrType(e error) ErrorType {
    56  	if xe, ok := e.(*xdsClientError); ok {
    57  		return xe.t
    58  	}
    59  	return ErrorTypeUnknown
    60  }