fserror_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. "testing"
  19. "yunion.io/x/pkg/errors"
  20. )
  21. func TestFsErrorNormalize(t *testing.T) {
  22. cases := []struct {
  23. inErr error
  24. want error
  25. }{
  26. {
  27. inErr: fs.ErrInvalid,
  28. want: ErrInputParameter,
  29. },
  30. {
  31. inErr: fs.ErrPermission,
  32. want: ErrForbidden,
  33. },
  34. {
  35. inErr: fs.ErrExist,
  36. want: ErrConflict,
  37. },
  38. {
  39. inErr: fs.ErrNotExist,
  40. want: ErrNotFound,
  41. },
  42. {
  43. inErr: fs.ErrClosed,
  44. want: ErrInvalidStatus,
  45. },
  46. {
  47. inErr: os.ErrNoDeadline,
  48. want: ErrNotSupported,
  49. },
  50. {
  51. inErr: os.ErrDeadlineExceeded,
  52. want: ErrTimeout,
  53. },
  54. }
  55. for _, c := range cases {
  56. got := FsErrorNormalize(c.inErr)
  57. if errors.Cause(got) != c.want {
  58. t.Errorf("inErr %s want %s got %s", c.inErr, c.want, got)
  59. }
  60. }
  61. }