github.com/vmware/govmomi@v0.37.2/find/error.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     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 find
    18  
    19  import "fmt"
    20  
    21  type NotFoundError struct {
    22  	kind string
    23  	path string
    24  }
    25  
    26  func (e *NotFoundError) Error() string {
    27  	return fmt.Sprintf("%s '%s' not found", e.kind, e.path)
    28  }
    29  
    30  type MultipleFoundError struct {
    31  	kind string
    32  	path string
    33  }
    34  
    35  func (e *MultipleFoundError) Error() string {
    36  	return fmt.Sprintf("path '%s' resolves to multiple %ss", e.path, e.kind)
    37  }
    38  
    39  type DefaultNotFoundError struct {
    40  	kind string
    41  }
    42  
    43  func (e *DefaultNotFoundError) Error() string {
    44  	return fmt.Sprintf("no default %s found", e.kind)
    45  }
    46  
    47  type DefaultMultipleFoundError struct {
    48  	kind string
    49  }
    50  
    51  func (e DefaultMultipleFoundError) Error() string {
    52  	return fmt.Sprintf("default %s resolves to multiple instances, please specify", e.kind)
    53  }
    54  
    55  func toDefaultError(err error) error {
    56  	switch e := err.(type) {
    57  	case *NotFoundError:
    58  		return &DefaultNotFoundError{e.kind}
    59  	case *MultipleFoundError:
    60  		return &DefaultMultipleFoundError{e.kind}
    61  	default:
    62  		return err
    63  	}
    64  }