errors.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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 compose
  14. import (
  15. "io/fs"
  16. "github.com/pkg/errors"
  17. "github.com/compose-spec/compose-go/errdefs"
  18. )
  19. // Error error to categorize failures and extract metrics info
  20. type Error struct {
  21. Err error
  22. Category *FailureCategory
  23. }
  24. // WrapComposeError wraps the error if not nil, otherwise returns nil
  25. func WrapComposeError(err error) error {
  26. if err == nil {
  27. return nil
  28. }
  29. return Error{
  30. Err: err,
  31. }
  32. }
  33. // WrapCategorisedComposeError wraps the error if not nil, otherwise returns nil
  34. func WrapCategorisedComposeError(err error, failure FailureCategory) error {
  35. if err == nil {
  36. return nil
  37. }
  38. return Error{
  39. Err: err,
  40. Category: &failure,
  41. }
  42. }
  43. // Unwrap get underlying error
  44. func (e Error) Unwrap() error { return e.Err }
  45. func (e Error) Error() string { return e.Err.Error() }
  46. // GetMetricsFailureCategory get metrics status and error code corresponding to this error
  47. func (e Error) GetMetricsFailureCategory() FailureCategory {
  48. if e.Category != nil {
  49. return *e.Category
  50. }
  51. var pathError *fs.PathError
  52. if errors.As(e.Err, &pathError) {
  53. return FileNotFoundFailure
  54. }
  55. if errdefs.IsNotFoundError(e.Err) {
  56. return FileNotFoundFailure
  57. }
  58. return ComposeParseFailure
  59. }