errors.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 api
  14. import (
  15. "errors"
  16. )
  17. const (
  18. // ExitCodeLoginRequired exit code when command cannot execute because it requires cloud login
  19. // This will be used by VSCode to detect when creating context if the user needs to login first
  20. ExitCodeLoginRequired = 5
  21. )
  22. var (
  23. // ErrNotFound is returned when an object is not found
  24. ErrNotFound = errors.New("not found")
  25. // ErrAlreadyExists is returned when an object already exists
  26. ErrAlreadyExists = errors.New("already exists")
  27. // ErrForbidden is returned when an operation is not permitted
  28. ErrForbidden = errors.New("forbidden")
  29. // ErrUnknown is returned when the error type is unmapped
  30. ErrUnknown = errors.New("unknown")
  31. // ErrLoginFailed is returned when login failed
  32. ErrLoginFailed = errors.New("login failed")
  33. // ErrLoginRequired is returned when login is required for a specific action
  34. ErrLoginRequired = errors.New("login required")
  35. // ErrNotImplemented is returned when a backend doesn't implement
  36. // an action
  37. ErrNotImplemented = errors.New("not implemented")
  38. // ErrUnsupportedFlag is returned when a backend doesn't support a flag
  39. ErrUnsupportedFlag = errors.New("unsupported flag")
  40. // ErrCanceled is returned when the command was canceled by user
  41. ErrCanceled = errors.New("canceled")
  42. // ErrParsingFailed is returned when a string cannot be parsed
  43. ErrParsingFailed = errors.New("parsing failed")
  44. // ErrWrongContextType is returned when the caller tries to get a context
  45. // with the wrong type
  46. ErrWrongContextType = errors.New("wrong context type")
  47. )
  48. // IsNotFoundError returns true if the unwrapped error is ErrNotFound
  49. func IsNotFoundError(err error) bool {
  50. return errors.Is(err, ErrNotFound)
  51. }
  52. // IsAlreadyExistsError returns true if the unwrapped error is ErrAlreadyExists
  53. func IsAlreadyExistsError(err error) bool {
  54. return errors.Is(err, ErrAlreadyExists)
  55. }
  56. // IsForbiddenError returns true if the unwrapped error is ErrForbidden
  57. func IsForbiddenError(err error) bool {
  58. return errors.Is(err, ErrForbidden)
  59. }
  60. // IsUnknownError returns true if the unwrapped error is ErrUnknown
  61. func IsUnknownError(err error) bool {
  62. return errors.Is(err, ErrUnknown)
  63. }
  64. // IsErrUnsupportedFlag returns true if the unwrapped error is ErrUnsupportedFlag
  65. func IsErrUnsupportedFlag(err error) bool {
  66. return errors.Is(err, ErrUnsupportedFlag)
  67. }
  68. // IsErrNotImplemented returns true if the unwrapped error is ErrNotImplemented
  69. func IsErrNotImplemented(err error) bool {
  70. return errors.Is(err, ErrNotImplemented)
  71. }
  72. // IsErrParsingFailed returns true if the unwrapped error is ErrParsingFailed
  73. func IsErrParsingFailed(err error) bool {
  74. return errors.Is(err, ErrParsingFailed)
  75. }
  76. // IsErrCanceled returns true if the unwrapped error is ErrCanceled
  77. func IsErrCanceled(err error) bool {
  78. return errors.Is(err, ErrCanceled)
  79. }