From 4de21eaf40ab75090f673cf464f0994f96082f49 Mon Sep 17 00:00:00 2001 From: Felipe Martin Garcia <812088+fmartingr@users.noreply.github.com> Date: Fri, 14 Oct 2022 13:37:24 +0200 Subject: [PATCH] fix: avoid panic usage when downloading bookmark (#513) Removed the usage of `panic()` when downloading a bookmark and simply return an error that has to be checked by implementations. Right now the API will continue if the bookmark download fails (either sync or async) but will leave a log with the error cause, so the users have the bookmark stored even if the archival didn't actually happen (but can be done manually later on). Fixes #459 --- internal/webserver/handler-api.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/webserver/handler-api.go b/internal/webserver/handler-api.go index b66bb53..3d60e7c 100644 --- a/internal/webserver/handler-api.go +++ b/internal/webserver/handler-api.go @@ -26,7 +26,7 @@ import ( func downloadBookmarkContent(book *model.Bookmark, dataDir string, request *http.Request) (*model.Bookmark, error) { content, contentType, err := core.DownloadBookmark(book.URL) if err != nil { - return nil, fmt.Errorf("error downloading bookmark: %s", err) + return nil, fmt.Errorf("error downloading url: %s", err) } processRequest := core.ProcessRequest{ @@ -40,7 +40,7 @@ func downloadBookmarkContent(book *model.Bookmark, dataDir string, request *http content.Close() if err != nil && isFatalErr { - panic(fmt.Errorf("failed to process bookmark: %v", err)) + return nil, fmt.Errorf("failed to process: %v", err) } return &result, err @@ -328,6 +328,7 @@ func (h *handler) apiInsertBookmark(w http.ResponseWriter, r *http.Request, ps h bookmark, err := downloadBookmarkContent(book, h.DataDir, r) if err != nil { log.Printf("error downloading boorkmark: %s", err) + return } if _, err := h.DB.SaveBookmarks(context.Background(), false, *bookmark); err != nil { log.Printf("failed to save bookmark: %s", err) @@ -339,8 +340,7 @@ func (h *handler) apiInsertBookmark(w http.ResponseWriter, r *http.Request, ps h book, err = downloadBookmarkContent(book, h.DataDir, r) if err != nil { log.Printf("error downloading boorkmark: %s", err) - } - if _, err := h.DB.SaveBookmarks(ctx, false, *book); err != nil { + } else if _, err := h.DB.SaveBookmarks(ctx, false, *book); err != nil { log.Printf("failed to save bookmark: %s", err) } }