refactored error handling
This commit is contained in:
parent
ca43177d49
commit
a8890c1d2d
@ -19,11 +19,14 @@ func Run(rootPath string) {
|
|||||||
//FindMissingImages()
|
//FindMissingImages()
|
||||||
//UploadImages()
|
//UploadImages()
|
||||||
|
|
||||||
authentication.Logout(context.Piwigo)
|
_ = authentication.Logout(context.Piwigo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ScanLocalDirectories(context *AppContext) {
|
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 {
|
for _, node := range fileNodes {
|
||||||
logrus.Debugln("found path entry:", node.Key)
|
logrus.Debugln("found path entry:", node.Key)
|
||||||
}
|
}
|
||||||
@ -69,12 +72,18 @@ func configureContext(rootPath string) *AppContext {
|
|||||||
|
|
||||||
func loginToPiwigoAndConfigureContext(context *AppContext) {
|
func loginToPiwigoAndConfigureContext(context *AppContext) {
|
||||||
logrus.Infoln("Logging in to piwigo and getting chunk size configuration for uploads")
|
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)
|
initializeUploadChunkSize(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeUploadChunkSize(context *AppContext) {
|
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
|
context.ChunkSizeBytes = userStatus.Result.UploadFormChunkSize * 1024
|
||||||
logrus.Debugln(context.ChunkSizeBytes)
|
logrus.Debugln(context.ChunkSizeBytes)
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ScanLocalFileStructure(path string) map[string]FilesystemNode {
|
func ScanLocalFileStructure(path string) (map[string]FilesystemNode,error){
|
||||||
fileMap := make(map[string]FilesystemNode)
|
fileMap := make(map[string]FilesystemNode)
|
||||||
|
|
||||||
err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileMap
|
return fileMap, nil
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,14 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"go/types"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Login(context *PiwigoContext) {
|
func Login(context *PiwigoContext) error {
|
||||||
logrus.Debugf("Logging in to %s using user %s", context.Url, context.Username)
|
logrus.Debugf("Logging in to %s using user %s", context.Url, context.Username)
|
||||||
|
|
||||||
if !strings.HasPrefix(context.Url, "https") {
|
if !strings.HasPrefix(context.Url, "https") {
|
||||||
@ -29,26 +30,27 @@ func Login(context *PiwigoContext) {
|
|||||||
response, err := client.PostForm(context.Url, formData)
|
response, err := client.PostForm(context.Url, formData)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
logrus.Errorf("The HTTP request failed with error %s", err)
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var loginResponse LoginResponse
|
var loginResponse LoginResponse
|
||||||
if err := json.NewDecoder(response.Body).Decode(&loginResponse); err != nil {
|
if err := json.NewDecoder(response.Body).Decode(&loginResponse); err != nil {
|
||||||
logrus.Errorln(err)
|
logrus.Errorln(err)
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if loginResponse.Status != "ok" {
|
if loginResponse.Status != "ok" {
|
||||||
errorMessage := fmt.Sprintf("Login failed: %d - %s", loginResponse.ErrorNumber, loginResponse.Message)
|
errorMessage := fmt.Sprintf("Login failed: %d - %s", loginResponse.ErrorNumber, loginResponse.Message)
|
||||||
logrus.Errorf(errorMessage)
|
logrus.Errorln(errorMessage)
|
||||||
panic(errorMessage)
|
return types.Error{Msg: errorMessage}
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Infof("Login succeeded: %s", loginResponse.Status)
|
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)
|
logrus.Debugf("Logging out from %s", context.Url)
|
||||||
|
|
||||||
initializeCookieJarIfRequired(context)
|
initializeCookieJarIfRequired(context)
|
||||||
@ -61,7 +63,7 @@ func Logout(context *PiwigoContext) {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
logrus.Errorln("The HTTP request failed with error %s", err)
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var statusResponse LogoutResponse
|
var statusResponse LogoutResponse
|
||||||
@ -74,9 +76,11 @@ func Logout(context *PiwigoContext) {
|
|||||||
} else {
|
} else {
|
||||||
logrus.Infof("Successfully logged out from %s", context.Url)
|
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...")
|
logrus.Debugln("Getting current login state...")
|
||||||
|
|
||||||
@ -90,19 +94,22 @@ func GetStatus(context *PiwigoContext) *GetStatusResponse {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s\n", err)
|
logrus.Errorln("The HTTP request failed with error %s\n", err)
|
||||||
return nil
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var statusResponse GetStatusResponse
|
var statusResponse GetStatusResponse
|
||||||
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
|
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
|
||||||
logrus.Errorln(err)
|
logrus.Errorln(err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if statusResponse.Status != "ok" {
|
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) {
|
func initializeCookieJarIfRequired(context *PiwigoContext) {
|
||||||
|
Loading…
Reference in New Issue
Block a user