diff --git a/core/library.go b/core/library.go index f1153da26..9f145f577 100644 --- a/core/library.go +++ b/core/library.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io/fs" - "os" "path/filepath" "slices" "strconv" @@ -367,25 +366,14 @@ func (r *libraryRepositoryWrapper) validateLibraryPath(ctx context.Context, libr fsys, err := fileStore.FS() if err != nil { log.Warn(ctx, "Error validating library.path", "path", library.Path, err) - return fmt.Errorf("resources.library.validation.pathInvalid") + return errors.New(classifyLibraryPathError(err)) } // Check if root directory exists info, err := fs.Stat(fsys, ".") if err != nil { - // Parse the error message to check for "not a directory" log.Warn(ctx, "Error stating library.path", "path", library.Path, err) - errStr := err.Error() - if strings.Contains(errStr, "not a directory") || - strings.Contains(errStr, "The directory name is invalid.") { - return fmt.Errorf("resources.library.validation.pathNotDirectory") - } else if os.IsNotExist(err) { - return fmt.Errorf("resources.library.validation.pathNotFound") - } else if os.IsPermission(err) { - return fmt.Errorf("resources.library.validation.pathNotAccessible") - } else { - return fmt.Errorf("resources.library.validation.pathInvalid") - } + return errors.New(classifyLibraryPathError(err)) } if !info.IsDir() { @@ -395,6 +383,22 @@ func (r *libraryRepositoryWrapper) validateLibraryPath(ctx context.Context, libr return nil } +// classifyLibraryPathError maps a path error to its i18n key. It uses errors.Is because storage FS() wraps with %w. +// Not-a-directory is matched by text: syscall.ENOTDIR means ERROR_PATH_NOT_FOUND on Windows, and ERROR_DIRECTORY is not exported. +func classifyLibraryPathError(err error) string { + switch { + case errors.Is(err, fs.ErrNotExist): + return "resources.library.validation.pathNotFound" + case errors.Is(err, fs.ErrPermission): + return "resources.library.validation.pathNotAccessible" + case strings.Contains(err.Error(), "not a directory") || + strings.Contains(err.Error(), "The directory name is invalid."): + return "resources.library.validation.pathNotDirectory" + default: + return "resources.library.validation.pathInvalid" + } +} + func (s *libraryService) validateLibraryIDs(ctx context.Context, libraryIDs []int) error { if len(libraryIDs) == 0 { return nil diff --git a/core/library_internal_test.go b/core/library_internal_test.go new file mode 100644 index 000000000..a2e88a06d --- /dev/null +++ b/core/library_internal_test.go @@ -0,0 +1,29 @@ +package core + +import ( + "errors" + "fmt" + "io/fs" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("classifyLibraryPathError", func() { + DescribeTable("maps a filesystem error to a validation message key", + func(err error, expected string) { + Expect(classifyLibraryPathError(err)).To(Equal(expected)) + }, + Entry("missing path", fs.ErrNotExist, "resources.library.validation.pathNotFound"), + Entry("wrapped missing path", fmt.Errorf("stat /music: %w", fs.ErrNotExist), "resources.library.validation.pathNotFound"), + Entry("permission denied", fs.ErrPermission, "resources.library.validation.pathNotAccessible"), + Entry("wrapped permission denied", fmt.Errorf("open /music: %w", fs.ErrPermission), "resources.library.validation.pathNotAccessible"), + Entry("file instead of a directory on Unix", + &fs.PathError{Op: "open", Path: "/music/song.mp3", Err: errors.New("not a directory")}, + "resources.library.validation.pathNotDirectory"), + Entry("file instead of a directory on Windows", + &fs.PathError{Op: "open", Path: `C:\music\song.mp3`, Err: errors.New("The directory name is invalid.")}, + "resources.library.validation.pathNotDirectory"), + Entry("any other error", errors.New("input/output error"), "resources.library.validation.pathInvalid"), + ) +}) diff --git a/core/library_test.go b/core/library_test.go index e6ebb1974..145d31e68 100644 --- a/core/library_test.go +++ b/core/library_test.go @@ -375,7 +375,7 @@ var _ = Describe("Library Service", func() { Expect(err).To(HaveOccurred()) var validationErr *rest.ValidationError Expect(errors.As(err, &validationErr)).To(BeTrue()) - Expect(validationErr.Errors["path"]).To(Equal("resources.library.validation.pathInvalid")) + Expect(validationErr.Errors["path"]).To(Equal("resources.library.validation.pathNotFound")) }) It("fails when path is a file instead of directory", func() { @@ -455,7 +455,7 @@ var _ = Describe("Library Service", func() { Expect(err).To(HaveOccurred()) var validationErr *rest.ValidationError Expect(errors.As(err, &validationErr)).To(BeTrue()) - Expect(validationErr.Errors["path"]).To(Equal("resources.library.validation.pathInvalid")) + Expect(validationErr.Errors["path"]).To(Equal("resources.library.validation.pathNotFound")) }) It("fails when updated path is a file instead of directory", func() {