dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/client/resource/errors.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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   *
    20   * Copyright 2021 gRPC authors.
    21   *
    22   */
    23  
    24  package resource
    25  
    26  import (
    27  	"fmt"
    28  )
    29  
    30  // ErrorType is the type of the error that the watcher will receive from the xds
    31  // client.
    32  type ErrorType int
    33  
    34  const (
    35  	// ErrorTypeUnknown indicates the error doesn't have a specific type. It is
    36  	// the default value, and is returned if the error is not an xds error.
    37  	ErrorTypeUnknown ErrorType = iota
    38  	// ErrorTypeConnection indicates a connection error from the gRPC client.
    39  	ErrorTypeConnection
    40  	// ErrorTypeResourceNotFound indicates a resource is not found from the xds
    41  	// response. It's typically returned if the resource is removed in the xds
    42  	// server.
    43  	ErrorTypeResourceNotFound
    44  )
    45  
    46  type xdsClientError struct {
    47  	t    ErrorType
    48  	desc string
    49  }
    50  
    51  func (e *xdsClientError) Error() string {
    52  	return e.desc
    53  }
    54  
    55  // NewErrorf creates an xds client error. The callbacks are called with this
    56  // error, to pass additional information about the error.
    57  func NewErrorf(t ErrorType, format string, args ...interface{}) error {
    58  	return &xdsClientError{t: t, desc: fmt.Sprintf(format, args...)}
    59  }
    60  
    61  // ErrType returns the error's type.
    62  func ErrType(e error) ErrorType {
    63  	if xe, ok := e.(*xdsClientError); ok {
    64  		return xe.t
    65  	}
    66  	return ErrorTypeUnknown
    67  }