Skip to content

Commit

Permalink
Better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
RadhiFadlillah committed May 28, 2018
1 parent 293cbbb commit b1ae14e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
76 changes: 44 additions & 32 deletions cmd/cmd-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (h *cmdHandler) addBookmark(cmd *cobra.Command, args []string) {
}

// Save bookmark to database
book.ID, err = h.db.CreateBookmark(book)
_, err = h.db.InsertBookmark(book)
if err != nil {
cError.Println(err)
return
Expand Down Expand Up @@ -281,6 +281,7 @@ func (h *cmdHandler) updateBookmarks(cmd *cobra.Command, args []string) {
}

// If not offline, fetch articles from internet
listErrorMsg := []string{}
if !offline {
fmt.Println("Fetching new bookmarks data")

Expand All @@ -306,12 +307,20 @@ func (h *cmdHandler) updateBookmarks(cmd *cobra.Command, args []string) {
// Parse URL
parsedURL, err := nurl.Parse(book.URL)
if err != nil || !valid.IsRequestURL(book.URL) {
mx.Lock()
errorMsg := fmt.Sprintf("Failed to fetch %s: URL is not valid", book.URL)
listErrorMsg = append(listErrorMsg, errorMsg)
mx.Unlock()
return
}

// Fetch data from internet
article, err := readability.FromURL(parsedURL, 20*time.Second)
if err != nil {
mx.Lock()
errorMsg := fmt.Sprintf("Failed to fetch %s: %v", book.URL, err)
listErrorMsg = append(listErrorMsg, errorMsg)
mx.Unlock()
return
}

Expand Down Expand Up @@ -342,7 +351,12 @@ func (h *cmdHandler) updateBookmarks(cmd *cobra.Command, args []string) {

wg.Wait()
uiprogress.Stop()
fmt.Println("\nSaving new data")

// Print error message
fmt.Println()
for _, errorMsg := range listErrorMsg {
cError.Println(errorMsg + "\n")
}
}

// Map the tags to be added or deleted from flag --tags
Expand Down Expand Up @@ -581,6 +595,17 @@ func (h *cmdHandler) importBookmarks(cmd *cobra.Command, args []string) {
intModified, _ := strconv.ParseInt(strModified, 10, 64)
modified := time.Unix(intModified, 0)

// Make sure URL valid
parsedURL, err := nurl.Parse(url)
if err != nil || !valid.IsRequestURL(url) {
cError.Printf("%s will be skipped: URL is not valid\n\n", url)
return
}

// Clear fragment and UTM parameters from URL
parsedURL.Fragment = ""
clearUTMParams(parsedURL)

// Get bookmark tags
tags := []model.Tag{}
for _, strTag := range strings.Split(strTags, ",") {
Expand Down Expand Up @@ -611,7 +636,7 @@ func (h *cmdHandler) importBookmarks(cmd *cobra.Command, args []string) {

// Add item to list
bookmark := model.Bookmark{
URL: url,
URL: parsedURL.String(),
Title: normalizeSpace(title),
Excerpt: normalizeSpace(excerpt),
Modified: modified.Format("2006-01-02 15:04:05"),
Expand All @@ -623,22 +648,10 @@ func (h *cmdHandler) importBookmarks(cmd *cobra.Command, args []string) {

// Save bookmarks to database
for _, book := range bookmarks {
// Make sure URL valid
parsedURL, err := nurl.Parse(book.URL)
if err != nil || !valid.IsRequestURL(book.URL) {
cError.Println("URL is not valid")
continue
}

// Clear fragment and UTM parameters from URL
parsedURL.Fragment = ""
clearUTMParams(parsedURL)
book.URL = parsedURL.String()

// Save book to database
book.ID, err = h.db.CreateBookmark(book)
book.ID, err = h.db.InsertBookmark(book)
if err != nil {
cError.Println(err)
cError.Printf("%s is skipped: %v\n\n", book.URL, err)
continue
}

Expand Down Expand Up @@ -747,6 +760,17 @@ func (h *cmdHandler) importPockets(cmd *cobra.Command, args []string) {
intModified, _ := strconv.ParseInt(strModified, 10, 64)
modified := time.Unix(intModified, 0)

// Make sure URL valid
parsedURL, err := nurl.Parse(url)
if err != nil || !valid.IsRequestURL(url) {
cError.Printf("%s will be skipped: URL is not valid\n\n", url)
return
}

// Clear fragment and UTM parameters from URL
parsedURL.Fragment = ""
clearUTMParams(parsedURL)

// Get bookmark tags
tags := []model.Tag{}
for _, strTag := range strings.Split(strTags, ",") {
Expand All @@ -757,7 +781,7 @@ func (h *cmdHandler) importPockets(cmd *cobra.Command, args []string) {

// Add item to list
bookmark := model.Bookmark{
URL: url,
URL: parsedURL.String(),
Title: normalizeSpace(title),
Modified: modified.Format("2006-01-02 15:04:05"),
Tags: tags,
Expand All @@ -768,22 +792,10 @@ func (h *cmdHandler) importPockets(cmd *cobra.Command, args []string) {

// Save bookmarks to database
for _, book := range bookmarks {
// Make sure URL valid
parsedURL, err := nurl.Parse(book.URL)
if err != nil || !valid.IsRequestURL(book.URL) {
cError.Println("URL is not valid")
continue
}

// Clear fragment and UTM parameters from URL
parsedURL.Fragment = ""
clearUTMParams(parsedURL)
book.URL = parsedURL.String()

// Save book to database
book.ID, err = h.db.CreateBookmark(book)
book.ID, err = h.db.InsertBookmark(book)
if err != nil {
cError.Println(err)
cError.Printf("%s is skipped: %v\n\n", book.URL, err)
continue
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/serve/web-handler-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (h *webHandler) apiInsertBookmark(w http.ResponseWriter, r *http.Request, p
}

// Save bookmark to database
_, err = h.db.CreateBookmark(book)
_, err = h.db.InsertBookmark(book)
checkError(err)

// Return new saved result
Expand Down
4 changes: 2 additions & 2 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

// Database is interface for manipulating data in database.
type Database interface {
// SaveBookmark saves new bookmark to database.
CreateBookmark(bookmark model.Bookmark) (int, error)
// InsertBookmark inserts new bookmark to database.
InsertBookmark(bookmark model.Bookmark) (int, error)

// GetBookmarks fetch list of bookmarks based on submitted ids.
GetBookmarks(withContent bool, ids ...int) ([]model.Bookmark, error)
Expand Down
6 changes: 3 additions & 3 deletions database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func OpenSQLiteDatabase(databasePath string) (*SQLiteDatabase, error) {
return &SQLiteDatabase{*db}, err
}

// CreateBookmark saves new bookmark to database. Returns new ID and error if any happened.
func (db *SQLiteDatabase) CreateBookmark(bookmark model.Bookmark) (bookmarkID int, err error) {
// InsertBookmark inserts new bookmark to database. Returns new ID and error if any happened.
func (db *SQLiteDatabase) InsertBookmark(bookmark model.Bookmark) (bookmarkID int, err error) {
// Check URL and title
if bookmark.URL == "" {
return -1, fmt.Errorf("URL must not be empty")
Expand All @@ -99,7 +99,7 @@ func (db *SQLiteDatabase) CreateBookmark(bookmark model.Bookmark) (bookmarkID in
bookmark.Modified = time.Now().UTC().Format("2006-01-02 15:04:05")
}

// Prepare transaction
// Begin transaction
tx, err := db.Beginx()
if err != nil {
return -1, err
Expand Down

0 comments on commit b1ae14e

Please sign in to comment.