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 (
|
||||
"encoding/json"
|
||||
"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 {
|
||||
Message string `json:"message"`
|
||||
|
@ -26,6 +36,7 @@ func (e encoder) StatusResponse(w http.ResponseWriter, status int, response any)
|
|||
w.WriteHeader(status)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
e.log.Error().Err(err).Msg("failed to encode response")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
@ -40,6 +51,7 @@ func (e encoder) StatusResponseMessage(w http.ResponseWriter, status int, messag
|
|||
w.WriteHeader(status)
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
@ -57,6 +69,7 @@ func (e encoder) StatusCreatedData(w http.ResponseWriter, data any) {
|
|||
w.WriteHeader(http.StatusCreated)
|
||||
|
||||
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)
|
||||
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.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)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e encoder) StatusInternalError(w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func (e encoder) Error(w http.ResponseWriter, err error) {
|
||||
res := errorResponse{
|
||||
Message: err.Error(),
|
||||
}
|
||||
|
||||
e.log.Error().Err(err).Msg("internal server error")
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(res); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
if encErr := json.NewEncoder(w).Encode(res); encErr != nil {
|
||||
e.log.Error().Err(encErr).Msg("failed to encode error response")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -107,10 +119,13 @@ func (e encoder) StatusError(w http.ResponseWriter, status int, 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.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)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue