Better error message

This commit is contained in:
Radhi Fadlillah 2018-05-28 21:22:17 +07:00
parent 293cbbb3bc
commit b1ae14e079
4 changed files with 50 additions and 38 deletions

View file

@ -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
}

View file

@ -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

View file

@ -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)

View file

@ -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