diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-10 09:43:39 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-10 09:43:39 +0300 |
| commit | 8b58c8c90f71eb0b11ad47202692dc4ab80d34d4 (patch) | |
| tree | 7ac8ccaefc8c9ac9100e9b279b686a7b85105ff6 /internal/api/handlers.go | |
| parent | 83cd87a04f65229346cb4f77f56eba5da0c668c1 (diff) | |
refactor(api): replace all writeJSON 500 error patterns with handleError helper
Diffstat (limited to 'internal/api/handlers.go')
| -rw-r--r-- | internal/api/handlers.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/internal/api/handlers.go b/internal/api/handlers.go index 9fcb780..029d78b 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -41,6 +41,28 @@ func forbidden(w http.ResponseWriter, message string) { writeError(w, http.StatusForbidden, message) } +// handleError maps service sentinel errors to the appropriate HTTP status +// and writes a JSON error response. It falls back to 500 for unknown errors. +func handleError(w http.ResponseWriter, err error) { + switch { + case errors.Is(err, service.ErrNotFound), + errors.Is(err, service.ErrShareNotFound), + errors.Is(err, service.ErrMediaNotFound): + notFound(w) + case errors.Is(err, service.ErrForbidden): + forbidden(w, "forbidden") + case errors.Is(err, service.ErrAlreadyBootstrapped): + forbidden(w, "bootstrap already complete") + case errors.Is(err, service.ErrInvalidCredentials): + writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "invalid credentials"}) + case errors.Is(err, service.ErrUnsupportedExtension), + errors.Is(err, service.ErrInvalidFeed): + badRequest(w, err.Error()) + default: + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + } +} + func readJSON(r *http.Request, dst interface{}) error { if r.Body == nil { return errors.New("missing body") |
