...

Source file src/google.golang.org/grpc/xds/internal/xdsclient/xdsresource/errors.go

Documentation: google.golang.org/grpc/xds/internal/xdsclient/xdsresource

     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  	// ErrorTypeResourceTypeUnsupported indicates the receipt of a message from
    38  	// the management server with resources of an unsupported resource type.
    39  	ErrorTypeResourceTypeUnsupported
    40  	// ErrTypeStreamFailedAfterRecv indicates an ADS stream error, after
    41  	// successful receipt of at least one message from the server.
    42  	ErrTypeStreamFailedAfterRecv
    43  )
    44  
    45  type xdsClientError struct {
    46  	t    ErrorType
    47  	desc string
    48  }
    49  
    50  func (e *xdsClientError) Error() string {
    51  	return e.desc
    52  }
    53  
    54  // NewErrorf creates an xds client error. The callbacks are called with this
    55  // error, to pass additional information about the error.
    56  func NewErrorf(t ErrorType, format string, args ...any) error {
    57  	return &xdsClientError{t: t, desc: fmt.Sprintf(format, args...)}
    58  }
    59  
    60  // ErrType returns the error's type.
    61  func ErrType(e error) ErrorType {
    62  	if xe, ok := e.(*xdsClientError); ok {
    63  		return xe.t
    64  	}
    65  	return ErrorTypeUnknown
    66  }
    67  

View as plain text