vitess.io/vitess@v0.16.2/go/vt/topo/consultopo/error.go (about)

     1  /*
     2  Copyright 2019 The Vitess 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 consultopo
    18  
    19  import (
    20  	"errors"
    21  	"net/url"
    22  
    23  	"context"
    24  
    25  	"vitess.io/vitess/go/vt/topo"
    26  )
    27  
    28  // Errors specific to this package.
    29  var (
    30  	// ErrBadResponse is returned from this package if the
    31  	// response from the consul server does not contain the data
    32  	// that the API promises. The consul client unmarshals JSON
    33  	// from the server into a Response struct that uses pointers,
    34  	// so we need to check for nil pointers, or else a misbehaving
    35  	// consul could cause us to panic.
    36  	ErrBadResponse = errors.New("consul request returned success, but response is missing required data")
    37  )
    38  
    39  // convertError converts a context error into a topo error. All errors
    40  // are either application-level errors, or context errors.
    41  func convertError(err error, nodePath string) error {
    42  	// Unwrap errors from the Go HTTP client.
    43  	if urlErr, ok := err.(*url.Error); ok {
    44  		err = urlErr.Err
    45  	}
    46  
    47  	// Convert specific sentinel values.
    48  	switch err {
    49  	case context.Canceled:
    50  		return topo.NewError(topo.Interrupted, nodePath)
    51  	case context.DeadlineExceeded:
    52  		return topo.NewError(topo.Timeout, nodePath)
    53  	}
    54  
    55  	return err
    56  }