refactored error handling
This commit is contained in:
parent
ca43177d49
commit
a8890c1d2d
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user