refactored errorhandling one more time :-D

This commit is contained in:
Philipp Häfelfinger 2019-02-24 23:27:31 +01:00
parent 6472eeba37
commit a897d82bfa
3 changed files with 28 additions and 11 deletions

View File

@ -1,21 +1,26 @@
package app
import (
"errors"
"flag"
"github.com/sirupsen/logrus"
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication"
"os"
)
var (
imagesRootPath = flag.String("imagesRootPath", "", "This is the images root path that should be mirrored to piwigo.")
piwigoUrl = flag.String("piwigoUrl", "", "The root url to your piwigo installation.")
piwigoUser = flag.String("piwigoUser", "", "The username to use during sync.")
piwigoPassword = flag.String("piwigoPassword", "", "This is password to the given username.")
imagesRootPath = flag.String("imagesRootPath", "", "This is the images root path that should be mirrored to piwigo.")
piwigoUrl = flag.String("piwigoUrl", "", "The root url to your piwigo installation.")
piwigoUser = flag.String("piwigoUser", "", "The username to use during sync.")
piwigoPassword = flag.String("piwigoPassword", "", "This is password to the given username.")
)
func Run() {
context := configureContext()
context, err := configureContext()
if err != nil {
os.Exit(1)
}
loginToPiwigoAndConfigureContext(context)
@ -62,9 +67,21 @@ func UploadImages() {
logrus.Warnln("Uploading missing images (NotImplemented)")
}
func configureContext() *AppContext {
func configureContext() (*AppContext, error) {
logrus.Infoln("Preparing application context and configuration")
if *piwigoUrl == "" {
return nil, errors.New("missing piwigo url!")
}
if *piwigoUser == "" {
return nil, errors.New("missing piwigo user!")
}
if *piwigoPassword == "" {
return nil, errors.New("missing piwigo password!")
}
context := new(AppContext)
context.LocalRootPath = *imagesRootPath
context.Piwigo = new(authentication.PiwigoContext)
@ -72,7 +89,7 @@ func configureContext() *AppContext {
context.Piwigo.Username = *piwigoUser
context.Piwigo.Password = *piwigoPassword
return context
return context, nil
}
func loginToPiwigoAndConfigureContext(context *AppContext) {

View File

@ -5,7 +5,7 @@ import (
"path/filepath"
)
func ScanLocalFileStructure(path string) (map[string]FilesystemNode,error){
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 {

View File

@ -2,9 +2,9 @@ package authentication
import (
"encoding/json"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"go/types"
"net/http"
"net/http/cookiejar"
"net/url"
@ -43,7 +43,7 @@ func Login(context *PiwigoContext) error {
if loginResponse.Status != "ok" {
errorMessage := fmt.Sprintf("Login failed: %d - %s", loginResponse.ErrorNumber, loginResponse.Message)
logrus.Errorln(errorMessage)
return types.Error{Msg: errorMessage}
return errors.New(errorMessage)
}
logrus.Infof("Login succeeded: %s", loginResponse.Status)
@ -106,7 +106,7 @@ func GetStatus(context *PiwigoContext) (*GetStatusResponse, error) {
if statusResponse.Status != "ok" {
errorMessage := fmt.Sprintf("Could not get session state from %s", context.Url)
logrus.Errorln(errorMessage)
return nil, types.Error{Msg: errorMessage}
return nil, errors.New(errorMessage)
}
return &statusResponse, nil