feat(http): improve error handling (#1006)

This commit is contained in:
ze0s 2023-07-02 14:03:39 +02:00 committed by GitHub
parent 5cdf68bc77
commit c361f23139
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 94 additions and 66 deletions

View file

@ -58,14 +58,12 @@ func (h apikeyHandler) store(w http.ResponseWriter, r *http.Request) {
)
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
// encode error
h.encoder.StatusInternalError(w)
h.encoder.Error(w, err)
return
}
if err := h.service.Store(ctx, &data); err != nil {
// encode error
h.encoder.StatusInternalError(w)
h.encoder.Error(w, err)
return
}
@ -74,8 +72,9 @@ func (h apikeyHandler) store(w http.ResponseWriter, r *http.Request) {
func (h apikeyHandler) delete(w http.ResponseWriter, r *http.Request) {
if err := h.service.Delete(r.Context(), chi.URLParam(r, "apikey")); err != nil {
h.encoder.StatusInternalError(w)
h.encoder.Error(w, err)
return
}
h.encoder.NoContent(w)
}