diff options
Diffstat (limited to 'internal/api')
| -rw-r--r-- | internal/api/handlers.go | 16 | ||||
| -rw-r--r-- | internal/api/handlers_more_test.go | 37 |
2 files changed, 53 insertions, 0 deletions
diff --git a/internal/api/handlers.go b/internal/api/handlers.go index 672891d..3ee4445 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -267,6 +267,14 @@ func (s *Server) handleSetCover(w http.ResponseWriter, r *http.Request) { return } if err := s.mediaSvc.RegenerateSetCover(r.Context(), setID, userIDFromContext(r)); err != nil { + if errors.Is(err, service.ErrNotFound) { + writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"}) + return + } + if errors.Is(err, service.ErrForbidden) { + writeJSON(w, http.StatusForbidden, map[string]string{"error": "forbidden"}) + return + } writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) return } @@ -597,6 +605,14 @@ func (s *Server) handleRegenThumbnail(w http.ResponseWriter, r *http.Request) { return } if err := s.mediaSvc.RegenerateThumbnail(r.Context(), id, userIDFromContext(r)); err != nil { + if errors.Is(err, service.ErrNotFound) { + writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"}) + return + } + if errors.Is(err, service.ErrForbidden) { + writeJSON(w, http.StatusForbidden, map[string]string{"error": "forbidden"}) + return + } writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) return } diff --git a/internal/api/handlers_more_test.go b/internal/api/handlers_more_test.go index d2ef424..263041c 100644 --- a/internal/api/handlers_more_test.go +++ b/internal/api/handlers_more_test.go @@ -335,6 +335,8 @@ func TestServer_SetCover(t *testing.T) { {"nil service", "1", true, nil, http.StatusNotImplemented}, {"invalid id", "abc", false, nil, http.StatusBadRequest}, {"service error", "1", false, errors.New("boom"), http.StatusInternalServerError}, + {"not found", "1", false, service.ErrNotFound, http.StatusNotFound}, + {"forbidden", "1", false, service.ErrForbidden, http.StatusForbidden}, {"ok", "1", false, nil, http.StatusOK}, } @@ -743,6 +745,41 @@ func TestServer_RegenThumbnail(t *testing.T) { } } +func TestServer_RegenThumbnail_errorMapping(t *testing.T) { + store := buildSessionStore(1) + sm := auth.NewSessionManager(store, &clock.MockClock{T: time.Now()}, time.Hour) + cfg := &internal.Config{SessionTimeoutHours: 24} + + tests := []struct { + name string + svcErr error + wantCode int + }{ + {"not found", service.ErrNotFound, http.StatusNotFound}, + {"forbidden", service.ErrForbidden, http.StatusForbidden}, + {"internal error", errors.New("boom"), http.StatusInternalServerError}, + {"ok", nil, http.StatusOK}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ms := &service.MockMediaService{ + RegenerateThumbnailFunc: func(ctx context.Context, mediaID, userID int64) error { + return tt.svcErr + }, + } + srv := newTestServer(t, store, nil, sm, cfg, ms, nil, nil, nil) + req := httptest.NewRequest(http.MethodPost, "/api/media/1/thumbnail", nil) + req.AddCookie(sessionCookieForStore(t, store, sm, 1)) + rr := httptest.NewRecorder() + srv.ServeHTTP(rr, req) + if rr.Code != tt.wantCode { + t.Fatalf("expected %d, got %d", tt.wantCode, rr.Code) + } + }) + } +} + // ------------------------------------------------------------------ // Shares // ------------------------------------------------------------------ |
