[#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.String("request_id", rid),
zap.Error(err)) zap.Error(err))
api.WriteErrorResponse(r.Context(), w, api.Error{ api.WriteErrorResponse(r.Context(), w, err, r.URL)
Code: api.GetAPIError(api.ErrBadRequest).Code,
Description: err.Error(),
HTTPStatusCode: http.StatusBadRequest,
}, r.URL)
} }
// ListBucketsHandler handles bucket listing requests. // ListBucketsHandler handles bucket listing requests.

View file

@ -87,7 +87,6 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
var ( var (
err error err error
p = layer.CreateBucketParams{} p = layer.CreateBucketParams{}
rid = api.GetRequestID(r.Context())
req = mux.Vars(r) req = mux.Vars(r)
) )
p.Name = req["bucket"] p.Name = req["bucket"]
@ -98,19 +97,19 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
} }
if err != nil { 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 return
} }
createParams, err := parseLocationConstraint(r) createParams, err := parseLocationConstraint(r)
if err != nil { if err != nil {
h.writeError(w, r, "could not parse body", rid, err) h.registerAndSendError(w, r, err, "could not parse body")
return return
} }
boxData, err := getBoxData(r.Context()) boxData, err := getBoxData(r.Context())
if err != nil { if err != nil {
h.writeError(w, r, "could get boxData", rid, err) h.registerAndSendError(w, r, err, "could not get boxData")
return return
} }
@ -125,14 +124,14 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
if p.Policy == nil { if p.Policy == nil {
p.Policy, err = policy.Parse(defaultPolicy) p.Policy, err = policy.Parse(defaultPolicy)
if err != nil { if err != nil {
h.writeError(w, r, "could not parse policy", rid, err) h.registerAndSendError(w, r, err, "could not parse policy")
return return
} }
} }
cid, err := h.obj.CreateBucket(r.Context(), &p, boxData) cid, err := h.obj.CreateBucket(r.Context(), &p, boxData)
if err != nil { if err != nil {
h.writeError(w, r, "could not create bucket", rid, err) h.registerAndSendError(w, r, err, "could not create bucket")
return 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) { func getBoxData(ctx context.Context) (*accessbox.Box, error) {
var boxData *accessbox.Box var boxData *accessbox.Box
data, ok := ctx.Value(api.BoxData).(*accessbox.Box) data, ok := ctx.Value(api.BoxData).(*accessbox.Box)