refactored errorhandling one more time :-D
This commit is contained in:
parent
6472eeba37
commit
a897d82bfa
@ -1,10 +1,12 @@
|
||||
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 (
|
||||
@ -15,7 +17,10 @@ var (
|
||||
)
|
||||
|
||||
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) {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user