aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorRanXom <saiyedshizain@gmail.com>2026-07-13 17:53:46 +0530
committerRanXom <saiyedshizain@gmail.com>2026-07-13 17:53:46 +0530
commitf108043068c8818c616cc2e760f53839d4ba9b01 (patch)
tree01dce4dbdfd531f3eb60c920c3232c8bbafd43f4 /internal
parentd87995c3629cac4671b05073915214f45d0c7112 (diff)
downloadgalleryd-f108043068c8818c616cc2e760f53839d4ba9b01.tar.xz
galleryd-f108043068c8818c616cc2e760f53839d4ba9b01.zip
refactor(api): return structured error responses
Diffstat (limited to 'internal')
-rw-r--r--internal/api/error.go23
-rw-r--r--internal/api/health.go6
-rw-r--r--internal/api/photos.go12
3 files changed, 31 insertions, 10 deletions
diff --git a/internal/api/error.go b/internal/api/error.go
new file mode 100644
index 0000000..b00f326
--- /dev/null
+++ b/internal/api/error.go
@@ -0,0 +1,23 @@
+package api
+
+import (
+ "encoding/json"
+ "net/http"
+)
+
+type errorResponse struct {
+ Error string `json:"error"`
+}
+
+func writeError(
+ w http.ResponseWriter,
+ status int,
+ message string,
+) {
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(status)
+
+ _ = json.NewEncoder(w).Encode(errorResponse{
+ Error: message,
+ })
+}
diff --git a/internal/api/health.go b/internal/api/health.go
index 61d5185..814fef5 100644
--- a/internal/api/health.go
+++ b/internal/api/health.go
@@ -20,10 +20,6 @@ func (s *Server) handleHealth(
}
if err := json.NewEncoder(w).Encode(response); err != nil {
- http.Error(
- w,
- http.StatusText(http.StatusInternalServerError),
- http.StatusInternalServerError,
- )
+ writeError(w, http.StatusInternalServerError, "failed to encode response")
}
}
diff --git a/internal/api/photos.go b/internal/api/photos.go
index 92c5c04..ca6b038 100644
--- a/internal/api/photos.go
+++ b/internal/api/photos.go
@@ -12,12 +12,12 @@ func (s *Server) handlePhotos(
) {
photos, err := s.gallery.Gallery(r.Context())
if err != nil {
- log.Printf("gallery: %v", err)
+ log.Printf("build gallery: %v", err)
- http.Error(
+ writeError(
w,
- http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError,
+ "failed to build gallery",
)
return
}
@@ -38,10 +38,12 @@ func (s *Server) handlePhotos(
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
- http.Error(
+ log.Printf("encode photos response: %v", err)
+
+ writeError(
w,
- http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError,
+ "failed to encode response",
)
}
}