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,10 +1,12 @@
package app package app
import ( import (
"errors"
"flag" "flag"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication"
"os"
) )
var ( var (
@ -15,7 +17,10 @@ var (
) )
func Run() { func Run() {
context := configureContext() context, err := configureContext()
if err != nil {
os.Exit(1)
}
loginToPiwigoAndConfigureContext(context) loginToPiwigoAndConfigureContext(context)
@ -62,9 +67,21 @@ func UploadImages() {
logrus.Warnln("Uploading missing images (NotImplemented)") logrus.Warnln("Uploading missing images (NotImplemented)")
} }
func configureContext() *AppContext { func configureContext() (*AppContext, error) {
logrus.Infoln("Preparing application context and configuration") 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 := new(AppContext)
context.LocalRootPath = *imagesRootPath context.LocalRootPath = *imagesRootPath
context.Piwigo = new(authentication.PiwigoContext) context.Piwigo = new(authentication.PiwigoContext)
@ -72,7 +89,7 @@ func configureContext() *AppContext {
context.Piwigo.Username = *piwigoUser context.Piwigo.Username = *piwigoUser
context.Piwigo.Password = *piwigoPassword context.Piwigo.Password = *piwigoPassword
return context return context, nil
} }
func loginToPiwigoAndConfigureContext(context *AppContext) { func loginToPiwigoAndConfigureContext(context *AppContext) {

View File

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

View File

@ -2,9 +2,9 @@ package authentication
import ( import (
"encoding/json" "encoding/json"
"errors"
"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"
@ -43,7 +43,7 @@ func Login(context *PiwigoContext) error {
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.Errorln(errorMessage) logrus.Errorln(errorMessage)
return types.Error{Msg: errorMessage} return errors.New(errorMessage)
} }
logrus.Infof("Login succeeded: %s", loginResponse.Status) logrus.Infof("Login succeeded: %s", loginResponse.Status)
@ -106,7 +106,7 @@ func GetStatus(context *PiwigoContext) (*GetStatusResponse, error) {
if statusResponse.Status != "ok" { if statusResponse.Status != "ok" {
errorMessage := fmt.Sprintf("Could not get session state from %s", context.Url) errorMessage := fmt.Sprintf("Could not get session state from %s", context.Url)
logrus.Errorln(errorMessage) logrus.Errorln(errorMessage)
return nil, types.Error{Msg: errorMessage} return nil, errors.New(errorMessage)
} }
return &statusResponse, nil return &statusResponse, nil