fserror.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package httperrors
  15. import (
  16. "io/fs"
  17. "os"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. /*
  21. ErrInvalid = fs.ErrInvalid // "invalid argument"
  22. ErrPermission = fs.ErrPermission // "permission denied"
  23. ErrExist = fs.ErrExist // "file already exists"
  24. ErrNotExist = fs.ErrNotExist // "file does not exist"
  25. ErrClosed = fs.ErrClosed // "file already closed"
  26. ErrNoDeadline = errNoDeadline() // "file type does not support deadline"
  27. ErrDeadlineExceeded = errDeadlineExceeded() // "i/o timeout"
  28. */
  29. func FsErrorNormalize(err error) error {
  30. switch errors.Cause(err) {
  31. case fs.ErrInvalid:
  32. return errors.Wrap(ErrInputParameter, "invalid argument")
  33. case fs.ErrPermission:
  34. return errors.Wrap(ErrForbidden, "permission denied")
  35. case fs.ErrExist:
  36. return errors.Wrap(ErrConflict, "file already exists")
  37. case fs.ErrNotExist:
  38. return errors.Wrap(ErrNotFound, "file does not exist")
  39. case fs.ErrClosed:
  40. return errors.Wrap(ErrInvalidStatus, "file already closed")
  41. case os.ErrNoDeadline:
  42. return errors.Wrap(ErrNotSupported, "file type does not support deadline")
  43. case os.ErrDeadlineExceeded:
  44. return errors.Wrap(ErrTimeout, "i/o timeout")
  45. }
  46. return err
  47. }