From a8890c1d2df34520642fdba828d7efc17094bb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sun, 24 Feb 2019 21:38:28 +0100 Subject: [PATCH] refactored error handling --- internal/app/app.go | 17 +++++++--- .../localFileStructure/filesystemScanner.go | 6 ++-- .../piwigo/authentication/authentication.go | 31 ++++++++++++------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 85d0460..945e230 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -19,11 +19,14 @@ func Run(rootPath string) { //FindMissingImages() //UploadImages() - authentication.Logout(context.Piwigo) + _ = authentication.Logout(context.Piwigo) } func ScanLocalDirectories(context *AppContext) { - var fileNodes map[string]localFileStructure.FilesystemNode = localFileStructure.ScanLocalFileStructure(context.LocalRootPath) + fileNodes, err := localFileStructure.ScanLocalFileStructure(context.LocalRootPath) + if err != nil { + panic(err) + } for _, node := range fileNodes { logrus.Debugln("found path entry:", node.Key) } @@ -69,12 +72,18 @@ func configureContext(rootPath string) *AppContext { func loginToPiwigoAndConfigureContext(context *AppContext) { logrus.Infoln("Logging in to piwigo and getting chunk size configuration for uploads") - authentication.Login(context.Piwigo) + err := authentication.Login(context.Piwigo) + if err != nil { + panic(err) + } initializeUploadChunkSize(context) } func initializeUploadChunkSize(context *AppContext) { - userStatus := authentication.GetStatus(context.Piwigo) + userStatus, err := authentication.GetStatus(context.Piwigo) + if err != nil { + panic(err) + } context.ChunkSizeBytes = userStatus.Result.UploadFormChunkSize * 1024 logrus.Debugln(context.ChunkSizeBytes) } diff --git a/internal/pkg/localFileStructure/filesystemScanner.go b/internal/pkg/localFileStructure/filesystemScanner.go index b98204b..498572a 100644 --- a/internal/pkg/localFileStructure/filesystemScanner.go +++ b/internal/pkg/localFileStructure/filesystemScanner.go @@ -5,7 +5,7 @@ import ( "path/filepath" ) -func ScanLocalFileStructure(path string) map[string]FilesystemNode { +func ScanLocalFileStructure(path string) (map[string]FilesystemNode,error){ fileMap := make(map[string]FilesystemNode) err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error { @@ -24,8 +24,8 @@ func ScanLocalFileStructure(path string) map[string]FilesystemNode { }) if err != nil { - panic(err) + return nil, err } - return fileMap + return fileMap, nil } diff --git a/internal/pkg/piwigo/authentication/authentication.go b/internal/pkg/piwigo/authentication/authentication.go index c448f60..38dc764 100644 --- a/internal/pkg/piwigo/authentication/authentication.go +++ b/internal/pkg/piwigo/authentication/authentication.go @@ -4,13 +4,14 @@ import ( "encoding/json" "fmt" "github.com/sirupsen/logrus" + "go/types" "net/http" "net/http/cookiejar" "net/url" "strings" ) -func Login(context *PiwigoContext) { +func Login(context *PiwigoContext) error { logrus.Debugf("Logging in to %s using user %s", context.Url, context.Username) if !strings.HasPrefix(context.Url, "https") { @@ -29,26 +30,27 @@ func Login(context *PiwigoContext) { response, err := client.PostForm(context.Url, formData) if err != nil { - logrus.Errorln("The HTTP request failed with error %s", err) - panic(err) + logrus.Errorf("The HTTP request failed with error %s", err) + return err } var loginResponse LoginResponse if err := json.NewDecoder(response.Body).Decode(&loginResponse); err != nil { logrus.Errorln(err) - panic(err) + return err } if loginResponse.Status != "ok" { errorMessage := fmt.Sprintf("Login failed: %d - %s", loginResponse.ErrorNumber, loginResponse.Message) - logrus.Errorf(errorMessage) - panic(errorMessage) + logrus.Errorln(errorMessage) + return types.Error{Msg: errorMessage} } logrus.Infof("Login succeeded: %s", loginResponse.Status) + return nil } -func Logout(context *PiwigoContext) { +func Logout(context *PiwigoContext) error { logrus.Debugf("Logging out from %s", context.Url) initializeCookieJarIfRequired(context) @@ -61,7 +63,7 @@ func Logout(context *PiwigoContext) { if err != nil { logrus.Errorln("The HTTP request failed with error %s", err) - return + return err } var statusResponse LogoutResponse @@ -74,9 +76,11 @@ func Logout(context *PiwigoContext) { } else { logrus.Infof("Successfully logged out from %s", context.Url) } + + return nil } -func GetStatus(context *PiwigoContext) *GetStatusResponse { +func GetStatus(context *PiwigoContext) (*GetStatusResponse, error) { logrus.Debugln("Getting current login state...") @@ -90,19 +94,22 @@ func GetStatus(context *PiwigoContext) *GetStatusResponse { if err != nil { logrus.Errorln("The HTTP request failed with error %s\n", err) - return nil + return nil, err } var statusResponse GetStatusResponse if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil { logrus.Errorln(err) + return nil, err } if statusResponse.Status != "ok" { - logrus.Errorf("Could not get session state from %s", context.Url) + errorMessage := fmt.Sprintf("Could not get session state from %s", context.Url) + logrus.Errorln(errorMessage) + return nil, types.Error{Msg: errorMessage} } - return &statusResponse + return &statusResponse, nil } func initializeCookieJarIfRequired(context *PiwigoContext) {