From b1ae14e079779df7d7b3774123817dd7e40562c8 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 28 May 2018 21:22:17 +0700 Subject: [PATCH] Better error message --- cmd/cmd-handler.go | 76 +++++++++++++++++++++--------------- cmd/serve/web-handler-api.go | 2 +- database/database.go | 4 +- database/sqlite.go | 6 +-- 4 files changed, 50 insertions(+), 38 deletions(-) diff --git a/cmd/cmd-handler.go b/cmd/cmd-handler.go index 718bc61f1..816741185 100644 --- a/cmd/cmd-handler.go +++ b/cmd/cmd-handler.go @@ -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 @@ -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") @@ -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 } @@ -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 @@ -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, ",") { @@ -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"), @@ -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 } @@ -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, ",") { @@ -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, @@ -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 } diff --git a/cmd/serve/web-handler-api.go b/cmd/serve/web-handler-api.go index 902a65507..8c8cb8523 100644 --- a/cmd/serve/web-handler-api.go +++ b/cmd/serve/web-handler-api.go @@ -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 diff --git a/database/database.go b/database/database.go index 166b29a7f..9f90b99d1 100644 --- a/database/database.go +++ b/database/database.go @@ -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) diff --git a/database/sqlite.go b/database/sqlite.go index 8ac4cb3f7..2e992ddd9 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -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") @@ -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