storj/private/api/errorhandling.go
Vitalii Shpital 07c71e34c2 satellite/console{gen}: GetUsersProjects endpoint
Initial implementation of auto-generated GetUsersProjects endpoint

Change-Id: If41bff2ea3ff9cfc87afeda9e5e5b3f586cbab33
2022-02-10 08:03:02 +00:00

50 lines
1.0 KiB
Go

// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package api
import (
"encoding/json"
"net/http"
"go.uber.org/zap"
)
// HTTPError holds http error entity with http status and error itself.
type HTTPError struct {
Status int
Err error
}
// Error returns http error's string representation.
func (e HTTPError) Error() string {
return e.Err.Error()
}
// ServeError writes JSON error to response output stream.
func ServeError(log *zap.Logger, w http.ResponseWriter, status int, err error) {
msg := err.Error()
fields := []zap.Field{
zap.Int("code", status),
zap.String("message", msg),
zap.Error(err),
}
if status == http.StatusNoContent {
return
} else if status/100 == 5 { // Check for 5XX status.
log.Error("returning error to client", fields...)
} else {
log.Debug("returning error to client", fields...)
}
w.WriteHeader(status)
err = json.NewEncoder(w).Encode(map[string]string{
"error": msg,
})
if err != nil {
log.Debug("failed to write json error response", zap.Error(err))
}
}