Skip to content

Commit

Permalink
Refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kakashi authored and kakashi committed Apr 17, 2018
1 parent 9ba68ab commit 762c8a5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
4 changes: 3 additions & 1 deletion internalmain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/kkc/safari-books-downloader/safari"

"github.com/kkc/safari-books-downloader/ebook"
"github.com/kkc/safari-books-downloader/utils"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -75,7 +76,8 @@ func DownloadSafariBook(cmd *cobra.Command, args []string) {
password = viper.GetString("safari.password")
}
safari := safari.NewSafari()
result := safari.FetchBookById(bookid, username, password)
result, err := safari.FetchBookById(bookid, username, password)
utils.StopOnErr(err)
ebook := ebook.NewEbook(result)
ebook.Save(output)
}
Expand Down
61 changes: 43 additions & 18 deletions safari/safari.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"sync"
"time"

logrus "github.com/Sirupsen/logrus"
)

type ChapterMeta struct {
Expand Down Expand Up @@ -203,14 +205,27 @@ func (s *Safari) adjustOrderByChapterNumber(chapters map[int]Chapter) ([]Chapter
return chapters_slice, nil
}

func (s *Safari) FetchBookById(id string, username string, password string) []byte {
// Get result by using book id
func (s *Safari) FetchBookById(id string, username string, password string) ([]byte, error) {
// check input format

_ = s.authorizeUser(username, password)
_ = s.fetchMeta(id)
err := s.authorizeUser(username, password)
if err != nil {
return nil, err
}
err = s.fetchMeta(id)
if err != nil {
return nil, err
}
_ = s.fetchTOC(id)
_ = s.fetchChapters(id)
_ = s.fetchStylesheet(id)
err = s.fetchChapters(id)
if err != nil {
return nil, err
}
err = s.fetchStylesheet(id)
if err != nil {
return nil, err
}

var author []string
for _, a := range s.books[id].meta.Authors {
Expand All @@ -222,7 +237,10 @@ func (s *Safari) FetchBookById(id string, username string, password string) []by
publisher = append(publisher, p.Name)
}

chapters, _ := s.adjustOrderByChapterNumber(s.books[id].chapters)
chapters, err := s.adjustOrderByChapterNumber(s.books[id].chapters)
if err != nil {
return nil, err
}

response := &jsonBook{
Title: s.books[id].meta.Title,
Expand All @@ -238,11 +256,9 @@ func (s *Safari) FetchBookById(id string, username string, password string) []by

data, err := json.Marshal(response)
if err != nil {
fmt.Println("error:", err)
return nil, err
}
//d, _ := prettyprint(data)
//fmt.Printf("%s", d)
return data
return data, nil
}

type AuthResponse struct {
Expand All @@ -253,6 +269,7 @@ type AuthResponse struct {
Scope string `json:"scope"`
}

// Login safari and get the access token
func (s *Safari) authorizeUser(username string, password string) error {
// http request
uri := s.baseUrl + "/oauth2/access_token/"
Expand All @@ -265,47 +282,55 @@ func (s *Safari) authorizeUser(username string, password string) error {
}
resp, err := http.PostForm(uri, form)
if err != nil {
fmt.Println("errorination happened getting the response", err)
return err
}
fmt.Println("login successfully")
defer resp.Body.Close()

if resp.StatusCode != 200 {
return errors.New("Login fail, please double check your username and password")
}

logrus.Info("login successfully")
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

var stuff AuthResponse
err = json.Unmarshal(body, &stuff)
if err != nil {
fmt.Println(err)
return err
}

s.accessToken = stuff.AccessToken
return nil
}

// Fetch safari resources by given url
func (s *Safari) fetchResource(url string) (string, error) {
uri := s.baseUrl + "/" + url

fmt.Println("fetch uri " + uri + "with token " + s.accessToken)
logrus.Info("fetch uri " + uri + " with token " + s.accessToken)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
fmt.Println("errorination happened getting the response", err)
return "", err
return nil, err
}
req.Header.Set("Authorization", "Bearer "+s.accessToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
err = errors.New("Error: status code != 200, actual status code '" + resp.Status + "'")
return "", err
return nil, err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
return nil, err
}

return string(body), nil
Expand Down
20 changes: 20 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package utils

import (
"os"

logrus "github.com/Sirupsen/logrus"
)

// StopOnErr exits on any error after logging it.
func StopOnErr(err error) {
if err == nil {
return
}
defer os.Exit(-1)

newMessage := err.Error()
if newMessage != "" {
logrus.Error(newMessage)
}
}

0 comments on commit 762c8a5

Please sign in to comment.