mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(http): add error logging for API responses (#1819)
* feat(api): add error logging for API responses * fix: revert import order * feat(http): log msg * feat(http): remove extra server logger --------- Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
parent
74eea79215
commit
fc137f2077
2 changed files with 25 additions and 10 deletions
|
@ -6,9 +6,19 @@ package http
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
type encoder struct{}
|
type encoder struct {
|
||||||
|
log zerolog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func newEncoder(log zerolog.Logger) encoder {
|
||||||
|
return encoder{
|
||||||
|
log: log,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type errorResponse struct {
|
type errorResponse struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
|
@ -26,6 +36,7 @@ func (e encoder) StatusResponse(w http.ResponseWriter, status int, response any)
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||||
|
e.log.Error().Err(err).Msg("failed to encode response")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -40,6 +51,7 @@ func (e encoder) StatusResponseMessage(w http.ResponseWriter, status int, messag
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(statusResponse{Message: message}); err != nil {
|
if err := json.NewEncoder(w).Encode(statusResponse{Message: message}); err != nil {
|
||||||
|
e.log.Error().Err(err).Msg("failed to encode status response")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -57,6 +69,7 @@ func (e encoder) StatusCreatedData(w http.ResponseWriter, data any) {
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||||
|
e.log.Error().Err(err).Msg("failed to encode created data response")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -78,26 +91,25 @@ func (e encoder) NotFoundErr(w http.ResponseWriter, err error) {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(res); err != nil {
|
if encErr := json.NewEncoder(w).Encode(res); encErr != nil {
|
||||||
|
e.log.Error().Err(encErr).Msg("failed to encode not found error response")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e encoder) StatusInternalError(w http.ResponseWriter) {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e encoder) Error(w http.ResponseWriter, err error) {
|
func (e encoder) Error(w http.ResponseWriter, err error) {
|
||||||
res := errorResponse{
|
res := errorResponse{
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
e.log.Error().Err(err).Msg("internal server error")
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(res); err != nil {
|
if encErr := json.NewEncoder(w).Encode(res); encErr != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
e.log.Error().Err(encErr).Msg("failed to encode error response")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,10 +119,13 @@ func (e encoder) StatusError(w http.ResponseWriter, status int, err error) {
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
e.log.Error().Err(err).Int("status", status).Msg("server error")
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(res); err != nil {
|
if encErr := json.NewEncoder(w).Encode(res); encErr != nil {
|
||||||
|
e.log.Error().Err(encErr).Msg("failed to encode status error response")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,7 +125,7 @@ func (s Server) Handler() http.Handler {
|
||||||
|
|
||||||
r.Use(c.Handler)
|
r.Use(c.Handler)
|
||||||
|
|
||||||
encoder := encoder{}
|
encoder := newEncoder(s.log)
|
||||||
|
|
||||||
r.Route("/api", func(r chi.Router) {
|
r.Route("/api", func(r chi.Router) {
|
||||||
r.Route("/auth", newAuthHandler(encoder, s.log, s, s.config.Config, s.cookieStore, s.authService).Routes)
|
r.Route("/auth", newAuthHandler(encoder, s.log, s, s.config.Config, s.cookieStore, s.authService).Routes)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue