error.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Copyright (c) 2015 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package find
  14. import "fmt"
  15. type NotFoundError struct {
  16. kind string
  17. path string
  18. }
  19. func (e *NotFoundError) Error() string {
  20. return fmt.Sprintf("%s '%s' not found", e.kind, e.path)
  21. }
  22. type MultipleFoundError struct {
  23. kind string
  24. path string
  25. }
  26. func (e *MultipleFoundError) Error() string {
  27. return fmt.Sprintf("path '%s' resolves to multiple %ss", e.path, e.kind)
  28. }
  29. type DefaultNotFoundError struct {
  30. kind string
  31. }
  32. func (e *DefaultNotFoundError) Error() string {
  33. return fmt.Sprintf("no default %s found", e.kind)
  34. }
  35. type DefaultMultipleFoundError struct {
  36. kind string
  37. }
  38. func (e DefaultMultipleFoundError) Error() string {
  39. return fmt.Sprintf("default %s resolves to multiple instances, please specify", e.kind)
  40. }
  41. func toDefaultError(err error) error {
  42. switch e := err.(type) {
  43. case *NotFoundError:
  44. return &DefaultNotFoundError{e.kind}
  45. case *MultipleFoundError:
  46. return &DefaultMultipleFoundError{e.kind}
  47. default:
  48. return err
  49. }
  50. }