From ae9f318a3effdb63aca66434a13176c1ac1a96a7 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Mon, 17 Jan 2022 17:28:54 +0100 Subject: [PATCH] Add storage module error handling --- storage/storage.go | 18 +++++++++++------- storage/storage_test.go | 6 ++++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/storage/storage.go b/storage/storage.go index 9e898a5..24a34c6 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -8,8 +8,8 @@ import ( ) type IStorageProvider interface { - StoreRaw(bucketName string, objectName string, data []byte) string - StoreExisting(bucketName string, objectName string, existingFilePath string) string + StoreRaw(bucketName string, objectName string, data []byte) (string, error) + StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error) } type FileSystemStorageProvider struct { @@ -17,19 +17,23 @@ type FileSystemStorageProvider struct { basePath string } -func (sp FileSystemStorageProvider) StoreRaw(bucketName string, objectName string, data []byte) string { +func (sp FileSystemStorageProvider) StoreRaw(bucketName string, objectName string, data []byte) (string, error) { directoryPath := filepath.Join(sp.basePath, bucketName) - sp.fileSystem.MkdirAll(directoryPath, os.ModePerm) + if err := sp.fileSystem.MkdirAll(directoryPath, os.ModePerm); err != nil { + return "", err + } filePath := filepath.Join(directoryPath, objectName) - afero.WriteFile(sp.fileSystem, filePath, data, os.ModePerm) + if err := afero.WriteFile(sp.fileSystem, filePath, data, os.ModePerm); err != nil { + return "", err + } - return filePath + return filePath, nil } -func (sp FileSystemStorageProvider) StoreExisting(bucketName string, objectName string, existingFilePath string) string { +func (sp FileSystemStorageProvider) StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error) { bytesRead, _ := afero.ReadFile(sp.fileSystem, existingFilePath) return sp.StoreRaw(bucketName, objectName, bytesRead) diff --git a/storage/storage_test.go b/storage/storage_test.go index d0aa620..278e252 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -19,7 +19,8 @@ func TestFileSystemStorageProvider(t *testing.T) { basePath: "/tmp/foo/bar", } - finalPath := provider.StoreRaw("test", "test.bin", dummyData) + finalPath, err := provider.StoreRaw("test", "test.bin", dummyData) + assert.Nil(t, err) assert.Equal(t, "/tmp/foo/bar/test/test.bin", finalPath) exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin") @@ -39,7 +40,8 @@ func TestFileSystemStorageProvider(t *testing.T) { basePath: "/tmp/foo/bar", } - finalPath := provider.StoreExisting("test", "test.bin", "/tmp/existing.bin") + finalPath, err := provider.StoreExisting("test", "test.bin", "/tmp/existing.bin") + assert.Nil(t, err) assert.Equal(t, "/tmp/foo/bar/test/test.bin", finalPath) exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin")