Skip to content

Commit

Permalink
Use pkg/errors to wrap error responses
Browse files Browse the repository at this point in the history
This allows the upper layers to look at the *actual* error, instead of
only a string representation.
  • Loading branch information
jacksontj committed Jul 2, 2018
1 parent 058dffe commit 39dcc8f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/go-kit/kit/log/level"
"github.com/golang/snappy"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
)

Expand Down Expand Up @@ -410,7 +411,7 @@ func (t *TricksterHandler) buildRequestContext(w http.ResponseWriter, r *http.Re

// Get the params from the User request so we can inspect them and pass on to prometheus
if err := r.ParseForm(); err != nil {
return nil, fmt.Errorf("Unable to parse form: %v", err)
return nil, errors.Wrap(err, "unable to parse form")
}
ctx.RequestParams = r.Form

Expand All @@ -421,7 +422,7 @@ func (t *TricksterHandler) buildRequestContext(w http.ResponseWriter, r *http.Re
ctx.StepParam = ctx.RequestParams[upStep][0]
step, err := strconv.ParseInt(ctx.StepParam, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse parameter %q with value %q: %v", upStep, ctx.StepParam, err)
return nil, errors.Wrap(err, fmt.Sprintf("failed to parse parameter %q with value %q", upStep, ctx.StepParam))
}
if step <= 0 {
return nil, fmt.Errorf("step parameter %d <= 0, has to be positive", step)
Expand Down Expand Up @@ -453,7 +454,7 @@ func (t *TricksterHandler) buildRequestContext(w http.ResponseWriter, r *http.Re

reqStart, err := parseTime(ctx.RequestParams[upStart][0])
if err != nil {
return nil, fmt.Errorf("failed to parse parameter %q with value %q: %v", upStart, ctx.RequestParams[upStart][0], err)
return nil, errors.Wrap(err, fmt.Sprintf("failed to parse parameter %q with value %q", upStart, ctx.RequestParams[upStart][0]))
}

if len(ctx.RequestParams[upEnd]) == 0 {
Expand All @@ -462,7 +463,7 @@ func (t *TricksterHandler) buildRequestContext(w http.ResponseWriter, r *http.Re

reqEnd, err := parseTime(ctx.RequestParams[upEnd][0])
if err != nil {
return nil, fmt.Errorf("failed to parse parameter %q with value %q: %v", upEnd, ctx.RequestParams[upEnd][0], err)
return nil, errors.Wrap(err, fmt.Sprintf("failed to parse parameter %q with value %q", upEnd, ctx.RequestParams[upEnd][0]))
}

ctx.RequestExtents.Start, ctx.RequestExtents.End = alignStepBoundaries(reqStart.Unix()*1000, reqEnd.Unix()*1000, ctx.StepMS, ctx.Time)
Expand Down Expand Up @@ -505,7 +506,7 @@ func (t *TricksterHandler) buildRequestContext(w http.ResponseWriter, r *http.Re
// Marshall the cache payload into a PrometheusMatrixEnvelope struct
err = json.Unmarshal([]byte(cachedBody), &ctx.Matrix)
if err != nil {
return nil, fmt.Errorf("error unmarshalling cached data for key %q with content %q: %v", ctx.CacheKey, cachedBody, err)
return nil, errors.Wrap(err, fmt.Sprintf("error unmarshalling cached data for key %q with content %q", ctx.CacheKey, cachedBody))
}

// Get the Extents of the data in the cache
Expand Down

0 comments on commit 39dcc8f

Please sign in to comment.