mirror of
https://github.com/go-shiori/shiori.git
synced 2025-09-10 15:05:20 +08:00
Better error message
This commit is contained in:
parent
293cbbb3bc
commit
b1ae14e079
4 changed files with 50 additions and 38 deletions
|
@ -100,7 +100,7 @@ func (h *cmdHandler) addBookmark(cmd *cobra.Command, args []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save bookmark to database
|
// Save bookmark to database
|
||||||
book.ID, err = h.db.CreateBookmark(book)
|
_, err = h.db.InsertBookmark(book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Println(err)
|
cError.Println(err)
|
||||||
return
|
return
|
||||||
|
@ -281,6 +281,7 @@ func (h *cmdHandler) updateBookmarks(cmd *cobra.Command, args []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not offline, fetch articles from internet
|
// If not offline, fetch articles from internet
|
||||||
|
listErrorMsg := []string{}
|
||||||
if !offline {
|
if !offline {
|
||||||
fmt.Println("Fetching new bookmarks data")
|
fmt.Println("Fetching new bookmarks data")
|
||||||
|
|
||||||
|
@ -306,12 +307,20 @@ func (h *cmdHandler) updateBookmarks(cmd *cobra.Command, args []string) {
|
||||||
// Parse URL
|
// Parse URL
|
||||||
parsedURL, err := nurl.Parse(book.URL)
|
parsedURL, err := nurl.Parse(book.URL)
|
||||||
if err != nil || !valid.IsRequestURL(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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch data from internet
|
// Fetch data from internet
|
||||||
article, err := readability.FromURL(parsedURL, 20*time.Second)
|
article, err := readability.FromURL(parsedURL, 20*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
mx.Lock()
|
||||||
|
errorMsg := fmt.Sprintf("Failed to fetch %s: %v", book.URL, err)
|
||||||
|
listErrorMsg = append(listErrorMsg, errorMsg)
|
||||||
|
mx.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -342,7 +351,12 @@ func (h *cmdHandler) updateBookmarks(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
uiprogress.Stop()
|
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
|
// 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)
|
intModified, _ := strconv.ParseInt(strModified, 10, 64)
|
||||||
modified := time.Unix(intModified, 0)
|
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
|
// Get bookmark tags
|
||||||
tags := []model.Tag{}
|
tags := []model.Tag{}
|
||||||
for _, strTag := range strings.Split(strTags, ",") {
|
for _, strTag := range strings.Split(strTags, ",") {
|
||||||
|
@ -611,7 +636,7 @@ func (h *cmdHandler) importBookmarks(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
// Add item to list
|
// Add item to list
|
||||||
bookmark := model.Bookmark{
|
bookmark := model.Bookmark{
|
||||||
URL: url,
|
URL: parsedURL.String(),
|
||||||
Title: normalizeSpace(title),
|
Title: normalizeSpace(title),
|
||||||
Excerpt: normalizeSpace(excerpt),
|
Excerpt: normalizeSpace(excerpt),
|
||||||
Modified: modified.Format("2006-01-02 15:04:05"),
|
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
|
// Save bookmarks to database
|
||||||
for _, book := range bookmarks {
|
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
|
// Save book to database
|
||||||
book.ID, err = h.db.CreateBookmark(book)
|
book.ID, err = h.db.InsertBookmark(book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Println(err)
|
cError.Printf("%s is skipped: %v\n\n", book.URL, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -747,6 +760,17 @@ func (h *cmdHandler) importPockets(cmd *cobra.Command, args []string) {
|
||||||
intModified, _ := strconv.ParseInt(strModified, 10, 64)
|
intModified, _ := strconv.ParseInt(strModified, 10, 64)
|
||||||
modified := time.Unix(intModified, 0)
|
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
|
// Get bookmark tags
|
||||||
tags := []model.Tag{}
|
tags := []model.Tag{}
|
||||||
for _, strTag := range strings.Split(strTags, ",") {
|
for _, strTag := range strings.Split(strTags, ",") {
|
||||||
|
@ -757,7 +781,7 @@ func (h *cmdHandler) importPockets(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
// Add item to list
|
// Add item to list
|
||||||
bookmark := model.Bookmark{
|
bookmark := model.Bookmark{
|
||||||
URL: url,
|
URL: parsedURL.String(),
|
||||||
Title: normalizeSpace(title),
|
Title: normalizeSpace(title),
|
||||||
Modified: modified.Format("2006-01-02 15:04:05"),
|
Modified: modified.Format("2006-01-02 15:04:05"),
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
|
@ -768,22 +792,10 @@ func (h *cmdHandler) importPockets(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
// Save bookmarks to database
|
// Save bookmarks to database
|
||||||
for _, book := range bookmarks {
|
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
|
// Save book to database
|
||||||
book.ID, err = h.db.CreateBookmark(book)
|
book.ID, err = h.db.InsertBookmark(book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Println(err)
|
cError.Printf("%s is skipped: %v\n\n", book.URL, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -156,7 +156,7 @@ func (h *webHandler) apiInsertBookmark(w http.ResponseWriter, r *http.Request, p
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save bookmark to database
|
// Save bookmark to database
|
||||||
_, err = h.db.CreateBookmark(book)
|
_, err = h.db.InsertBookmark(book)
|
||||||
checkError(err)
|
checkError(err)
|
||||||
|
|
||||||
// Return new saved result
|
// Return new saved result
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
|
|
||||||
// Database is interface for manipulating data in database.
|
// Database is interface for manipulating data in database.
|
||||||
type Database interface {
|
type Database interface {
|
||||||
// SaveBookmark saves new bookmark to database.
|
// InsertBookmark inserts new bookmark to database.
|
||||||
CreateBookmark(bookmark model.Bookmark) (int, error)
|
InsertBookmark(bookmark model.Bookmark) (int, error)
|
||||||
|
|
||||||
// GetBookmarks fetch list of bookmarks based on submitted ids.
|
// GetBookmarks fetch list of bookmarks based on submitted ids.
|
||||||
GetBookmarks(withContent bool, ids ...int) ([]model.Bookmark, error)
|
GetBookmarks(withContent bool, ids ...int) ([]model.Bookmark, error)
|
||||||
|
|
|
@ -76,8 +76,8 @@ func OpenSQLiteDatabase(databasePath string) (*SQLiteDatabase, error) {
|
||||||
return &SQLiteDatabase{*db}, err
|
return &SQLiteDatabase{*db}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateBookmark saves new bookmark to database. Returns new ID and error if any happened.
|
// InsertBookmark inserts new bookmark to database. Returns new ID and error if any happened.
|
||||||
func (db *SQLiteDatabase) CreateBookmark(bookmark model.Bookmark) (bookmarkID int, err error) {
|
func (db *SQLiteDatabase) InsertBookmark(bookmark model.Bookmark) (bookmarkID int, err error) {
|
||||||
// Check URL and title
|
// Check URL and title
|
||||||
if bookmark.URL == "" {
|
if bookmark.URL == "" {
|
||||||
return -1, fmt.Errorf("URL must not be empty")
|
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")
|
bookmark.Modified = time.Now().UTC().Format("2006-01-02 15:04:05")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare transaction
|
// Begin transaction
|
||||||
tx, err := db.Beginx()
|
tx, err := db.Beginx()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return -1, err
|
||||||
|
|
Loading…
Add table
Reference in a new issue