google.golang.org/grpc@v1.72.2/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 (
    22  	"errors"
    23  	"fmt"
    24  )
    25  
    26  // ErrorType is the type of the error that the watcher will receive from the xds
    27  // client.
    28  type ErrorType int
    29  
    30  const (
    31  	// ErrorTypeUnknown indicates the error doesn't have a specific type. It is
    32  	// the default value, and is returned if the error is not an xds error.
    33  	ErrorTypeUnknown ErrorType = iota
    34  	// ErrorTypeConnection indicates a connection error from the gRPC client.
    35  	ErrorTypeConnection
    36  	// ErrorTypeResourceNotFound indicates a resource is not found from the xds
    37  	// response. It's typically returned if the resource is removed in the xds
    38  	// server.
    39  	ErrorTypeResourceNotFound
    40  	// ErrorTypeResourceTypeUnsupported indicates the receipt of a message from
    41  	// the management server with resources of an unsupported resource type.
    42  	ErrorTypeResourceTypeUnsupported
    43  	// ErrTypeStreamFailedAfterRecv indicates an ADS stream error, after
    44  	// successful receipt of at least one message from the server.
    45  	ErrTypeStreamFailedAfterRecv
    46  	// ErrorTypeNACKed indicates that configuration provided by the xDS management
    47  	// server was NACKed.
    48  	ErrorTypeNACKed
    49  )
    50  
    51  type xdsClientError struct {
    52  	t    ErrorType
    53  	desc string
    54  }
    55  
    56  func (e *xdsClientError) Error() string {
    57  	return e.desc
    58  }
    59  
    60  // NewErrorf creates an xDS client error. The callbacks are called with this
    61  // error, to pass additional information about the error.
    62  func NewErrorf(t ErrorType, format string, args ...any) error {
    63  	return &xdsClientError{t: t, desc: fmt.Sprintf(format, args...)}
    64  }
    65  
    66  // NewError creates an xDS client error. The callbacks are called with this
    67  // error, to pass additional information about the error.
    68  func NewError(t ErrorType, message string) error {
    69  	return NewErrorf(t, "%s", message)
    70  }
    71  
    72  // ErrType returns the error's type.
    73  func ErrType(err error) ErrorType {
    74  	var xe *xdsClientError
    75  	if errors.As(err, &xe) {
    76  		return xe.t
    77  	}
    78  	return ErrorTypeUnknown
    79  }