Skip to content

Commit

Permalink
feat: use http.Error in responses
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Jan 8, 2025
1 parent e32d2af commit 0257dba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 4 additions & 2 deletions internal/apis/applemusic/applemusic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package applemusic

import (
"encoding/json"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -108,8 +109,9 @@ func serveHTTP(c *cache.Cache[cacheData]) http.HandlerFunc {
Encode(cache.CacheResponse[cacheDataResponse]{Data: data, Updated: c.Updated})
c.DataMutex.RUnlock()
if err != nil {
lumber.Error(err, "failed to write json data to request")
w.WriteHeader(http.StatusInternalServerError)
err = fmt.Errorf("%v failed to write json data to request", err)
lumber.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
}
5 changes: 3 additions & 2 deletions internal/apis/applemusic/playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ func playlistEndpoint(c *cache.Cache[cacheData]) http.HandlerFunc {
err := json.NewEncoder(w).Encode(p)
c.DataMutex.RUnlock()
if err != nil {
lumber.Error(err, "failed to write json data to request")
w.WriteHeader(http.StatusInternalServerError)
err = fmt.Errorf("%v failed to write json data to request", err)
lumber.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
}
5 changes: 3 additions & 2 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func (c *Cache[T]) ServeHTTP() http.HandlerFunc {
err := json.NewEncoder(w).Encode(CacheResponse[T]{Data: c.Data, Updated: c.Updated})
c.DataMutex.RUnlock()
if err != nil {
lumber.Error(err, "failed to write json data to request")
w.WriteHeader(http.StatusInternalServerError)
err = fmt.Errorf("%v failed to write json data to request", err)
lumber.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
}
Expand Down

0 comments on commit 0257dba

Please sign in to comment.