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

     1  /*
     2  Copyright 2020 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 k8stopo
    18  
    19  import (
    20  	"context"
    21  
    22  	"k8s.io/apimachinery/pkg/api/errors"
    23  
    24  	"vitess.io/vitess/go/vt/topo"
    25  )
    26  
    27  // convertError converts errors into a topo error. All errors
    28  // are either application-level errors, or context errors.
    29  func convertError(err error, nodePath string) error {
    30  	if err == nil {
    31  		return nil
    32  	}
    33  
    34  	// Check for specific kubernetes errors
    35  	if errors.IsAlreadyExists(err) {
    36  		return topo.NewError(topo.NodeExists, nodePath)
    37  	}
    38  	if errors.IsNotFound(err) {
    39  		return topo.NewError(topo.NoNode, nodePath)
    40  	}
    41  	if errors.IsServerTimeout(err) {
    42  		return topo.NewError(topo.Timeout, nodePath)
    43  	}
    44  
    45  	// Convert specific context sentinel values.
    46  	switch err {
    47  	case context.Canceled:
    48  		return topo.NewError(topo.Interrupted, nodePath)
    49  	case context.DeadlineExceeded:
    50  		return topo.NewError(topo.Timeout, nodePath)
    51  	}
    52  
    53  	return err
    54  }