From d4f96c6cdea4fb589a5df948ecddc40a940768aa Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Mon, 25 Dec 2023 12:06:02 +0100 Subject: [PATCH] fix handling of not found --- cmd/api-service/server/movie.go | 8 +++++++- cmd/api-service/server/sqlite.go | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/api-service/server/movie.go b/cmd/api-service/server/movie.go index 2e0d8ce..0686fe7 100644 --- a/cmd/api-service/server/movie.go +++ b/cmd/api-service/server/movie.go @@ -111,7 +111,13 @@ func (api *MovieAPI) Store(w http.ResponseWriter, r *http.Request, urlID string) func (api *MovieAPI) Delete(w http.ResponseWriter, r *http.Request, urlID string) { logger := api.logger.With("method", "delete") - if err := api.repo.Delete(urlID); err != nil { + err := api.repo.Delete(urlID) + switch { + case errors.Is(err, sql.ErrNoRows): + w.WriteHeader(http.StatusNotFound) + fmt.Fprint(w, `{"message":"not found"}`) + return + case err != nil: Error(w, http.StatusInternalServerError, "could not delete movie", err, logger) return } diff --git a/cmd/api-service/server/sqlite.go b/cmd/api-service/server/sqlite.go index 3ee8253..ad92534 100644 --- a/cmd/api-service/server/sqlite.go +++ b/cmd/api-service/server/sqlite.go @@ -125,7 +125,7 @@ WHERE id=?`, id) } var directors string if err := row.Scan(&m.ID, &m.TMDBID, &m.IMDBID, &m.Title, &m.EnglishTitle, &m.Year, &directors, &m.Summary, &m.WatchedOn, &m.Rating, &m.Comment); err != nil { - return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) + return nil, fmt.Errorf("%w: %w", ErrSqliteFailure, err) } m.Directors = strings.Split(directors, ",")