Skip to content

Commit b891f58

Browse files
fixed wrapError handlers (#81)
1 parent 42c57d3 commit b891f58

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

controllers/grants.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ func ClaimGrant(service *grant.Service) handlers.AppHandler {
5353

5454
body, err := ioutil.ReadAll(r.Body)
5555
if err != nil {
56-
return handlers.WrapError("Error reading body", err)
56+
return handlers.WrapError(err, "Error reading body", 0)
5757
}
5858

5959
var req grant.ClaimGrantRequest
6060
err = json.Unmarshal(body, &req)
6161
if err != nil {
62-
return handlers.WrapError("Error unmarshalling body", err)
62+
return handlers.WrapError(err, "Error unmarshalling body", 0)
6363
}
6464
_, err = govalidator.ValidateStruct(req)
6565
if err != nil {
@@ -82,7 +82,7 @@ func ClaimGrant(service *grant.Service) handlers.AppHandler {
8282
err = service.Claim(r.Context(), &req, grantID)
8383
if err != nil {
8484
// FIXME not all errors are 4xx
85-
return handlers.WrapError("Error claiming grant", err)
85+
return handlers.WrapError(err, "Error claiming grant", 0)
8686
}
8787
}
8888

@@ -98,13 +98,13 @@ func RedeemGrants(service *grant.Service) handlers.AppHandler {
9898

9999
body, err := ioutil.ReadAll(r.Body)
100100
if err != nil {
101-
return handlers.WrapError("Error reading body", err)
101+
return handlers.WrapError(err, "Error reading body", 0)
102102
}
103103

104104
var req grant.RedeemGrantsRequest
105105
err = json.Unmarshal(body, &req)
106106
if err != nil {
107-
return handlers.WrapError("Error unmarshalling body", err)
107+
return handlers.WrapError(err, "Error unmarshalling body", 0)
108108
}
109109
_, err = govalidator.ValidateStruct(req)
110110
if err != nil {
@@ -113,7 +113,7 @@ func RedeemGrants(service *grant.Service) handlers.AppHandler {
113113

114114
redeemedIDs, err := service.GetRedeemedIDs(r.Context(), req.Grants)
115115
if err != nil {
116-
return handlers.WrapError("Error checking grant redemption status", err)
116+
return handlers.WrapError(err, "Error checking grant redemption status", 0)
117117
}
118118

119119
if len(redeemedIDs) > 0 {
@@ -127,7 +127,7 @@ func RedeemGrants(service *grant.Service) handlers.AppHandler {
127127
txInfo, err := service.Redeem(r.Context(), &req)
128128
if err != nil {
129129
// FIXME not all errors are 4xx
130-
return handlers.WrapError("Error redeeming grant", err)
130+
return handlers.WrapError(err, "Error redeeming grant", 0)
131131
}
132132

133133
w.WriteHeader(http.StatusOK)

promotion/controllers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func GetAvailablePromotions(service *Service) handlers.AppHandler {
8282

8383
promotions, err := service.GetAvailablePromotions(r.Context(), id)
8484
if err != nil {
85-
return handlers.WrapError("Error getting available promotions", err)
85+
return handlers.WrapError(err, "Error getting available promotions", 0)
8686
}
8787

8888
w.WriteHeader(http.StatusOK)
@@ -110,13 +110,13 @@ func ClaimPromotion(service *Service) handlers.AppHandler {
110110
limit := int64(1024 * 1024 * 10) // 10MiB
111111
body, err := ioutil.ReadAll(io.LimitReader(r.Body, limit))
112112
if err != nil {
113-
return handlers.WrapError("Error reading body", err)
113+
return handlers.WrapError(err, "Error reading body", 0)
114114
}
115115

116116
var req ClaimRequest
117117
err = json.Unmarshal(body, &req)
118118
if err != nil {
119-
return handlers.WrapError("Error unmarshalling body", err)
119+
return handlers.WrapError(err, "Error unmarshalling body", 0)
120120
}
121121
_, err = govalidator.ValidateStruct(req)
122122
if err != nil {
@@ -125,7 +125,7 @@ func ClaimPromotion(service *Service) handlers.AppHandler {
125125

126126
keyID, err := middleware.GetKeyID(r.Context())
127127
if err != nil {
128-
return handlers.WrapError("Error looking up http signature info", err)
128+
return handlers.WrapError(err, "Error looking up http signature info", 0)
129129
}
130130
if req.PaymentID.String() != keyID {
131131
return &handlers.AppError{
@@ -159,7 +159,7 @@ func ClaimPromotion(service *Service) handlers.AppHandler {
159159

160160
claimID, err := service.ClaimPromotionForWallet(r.Context(), pID, req.PaymentID, req.BlindedCreds)
161161
if err != nil {
162-
return handlers.WrapError("Error claiming promotion", err)
162+
return handlers.WrapError(err, "Error claiming promotion", 0)
163163
}
164164

165165
w.WriteHeader(http.StatusOK)
@@ -200,7 +200,7 @@ func GetClaim(service *Service) handlers.AppHandler {
200200

201201
claim, err := service.datastore.GetClaimCreds(id)
202202
if err != nil {
203-
return handlers.WrapError("Error getting claim", err)
203+
return handlers.WrapError(err, "Error getting claim", 0)
204204
}
205205

206206
if claim == nil {

utils/handlers/handlers.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@ func (e AppError) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2626
}
2727

2828
// WrapError with an additional message as an AppError
29-
func WrapError(msg string, err error) *AppError {
29+
func WrapError(err error, msg string, code int) *AppError {
3030
// FIXME err should probably be first
31-
return &AppError{Error: err, Message: msg, Code: http.StatusBadRequest}
31+
if code == 0 {
32+
code = http.StatusBadRequest
33+
}
34+
return &AppError{
35+
Error: err,
36+
Message: msg,
37+
Code: code,
38+
}
3239
}
3340

3441
// WrapValidationError from govalidator

0 commit comments

Comments
 (0)