errors.go 924 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import "errors"
  8. var (
  9. ErrGeneric = errors.New("generic error")
  10. ErrNoSuchFile = errors.New("no such file")
  11. ErrInvalid = errors.New("file is invalid")
  12. )
  13. func codeToError(code ErrorCode) error {
  14. switch code {
  15. case ErrorCodeNoError:
  16. return nil
  17. case ErrorCodeNoSuchFile:
  18. return ErrNoSuchFile
  19. case ErrorCodeInvalidFile:
  20. return ErrInvalid
  21. default:
  22. return ErrGeneric
  23. }
  24. }
  25. func errorToCode(err error) ErrorCode {
  26. switch {
  27. case err == nil:
  28. return ErrorCodeNoError
  29. case errors.Is(err, ErrNoSuchFile):
  30. return ErrorCodeNoSuchFile
  31. case errors.Is(err, ErrInvalid):
  32. return ErrorCodeInvalidFile
  33. default:
  34. return ErrorCodeGeneric
  35. }
  36. }