[#89] Refactor error logging

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-07-22 09:45:24 +03:00
parent bba0402a98
commit b695e6a3b4
2 changed files with 6 additions and 16 deletions

View file

@ -45,11 +45,7 @@ func (h *handler) registerAndSendError(w http.ResponseWriter, r *http.Request, e
zap.String("request_id", rid),
zap.Error(err))
api.WriteErrorResponse(r.Context(), w, api.Error{
Code: api.GetAPIError(api.ErrBadRequest).Code,
Description: err.Error(),
HTTPStatusCode: http.StatusBadRequest,
}, r.URL)
api.WriteErrorResponse(r.Context(), w, err, r.URL)
}
// ListBucketsHandler handles bucket listing requests.

View file

@ -87,7 +87,6 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
p = layer.CreateBucketParams{}
rid = api.GetRequestID(r.Context())
req = mux.Vars(r)
)
p.Name = req["bucket"]
@ -98,19 +97,19 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
}
if err != nil {
h.writeError(w, r, "could not parse basic ACL", rid, err)
h.registerAndSendError(w, r, err, "could not parse basic ACL")
return
}
createParams, err := parseLocationConstraint(r)
if err != nil {
h.writeError(w, r, "could not parse body", rid, err)
h.registerAndSendError(w, r, err, "could not parse body")
return
}
boxData, err := getBoxData(r.Context())
if err != nil {
h.writeError(w, r, "could get boxData", rid, err)
h.registerAndSendError(w, r, err, "could not get boxData")
return
}
@ -125,14 +124,14 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
if p.Policy == nil {
p.Policy, err = policy.Parse(defaultPolicy)
if err != nil {
h.writeError(w, r, "could not parse policy", rid, err)
h.registerAndSendError(w, r, err, "could not parse policy")
return
}
}
cid, err := h.obj.CreateBucket(r.Context(), &p, boxData)
if err != nil {
h.writeError(w, r, "could not create bucket", rid, err)
h.registerAndSendError(w, r, err, "could not create bucket")
return
}
@ -174,11 +173,6 @@ func parseBasicACL(basicACL string) (uint32, error) {
}
}
func (h *handler) writeError(w http.ResponseWriter, r *http.Request, msg, rid string, err error) {
h.log.Error(msg, zap.String("request_id", rid), zap.Error(err))
api.WriteErrorResponse(r.Context(), w, err, r.URL)
}
func getBoxData(ctx context.Context) (*accessbox.Box, error) {
var boxData *accessbox.Box
data, ok := ctx.Value(api.BoxData).(*accessbox.Box)