Skip to content

Commit d6572c5

Browse files
evqmaikelmclauflin
authored andcommitted
break up utils (#46)
* break up utils * combine error and message automatically * only augment message if error exists
1 parent d4d2978 commit d6572c5

File tree

17 files changed

+69
-66
lines changed

17 files changed

+69
-66
lines changed

bin/create-tokens/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"time"
1212

1313
"github.com/brave-intl/bat-go/grant"
14-
"github.com/brave-intl/bat-go/utils"
1514
"github.com/brave-intl/bat-go/utils/altcurrency"
15+
"github.com/brave-intl/bat-go/utils/prompt"
1616
"github.com/brave-intl/bat-go/utils/vaultsigner"
1717
"github.com/satori/go.uuid"
1818
"golang.org/x/crypto/ed25519"
@@ -135,7 +135,7 @@ func main() {
135135

136136
fmt.Printf("Will create %d tokens worth %f %s each for promotion %s, valid starting on %s and expiring on %s\n", *numGrants, *value, altCurrency.String(), promotionUUID, maturityDate.String(), expiryDate.String())
137137
fmt.Print("Continue? ")
138-
resp, err := utils.PromptBool()
138+
resp, err := prompt.Bool()
139139
if err != nil {
140140
log.Fatalln(err)
141141
}

bin/list-transactions/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"os"
88
"sort"
99

10-
"github.com/brave-intl/bat-go/utils"
1110
"github.com/brave-intl/bat-go/utils/altcurrency"
11+
"github.com/brave-intl/bat-go/utils/formatters"
1212
"github.com/brave-intl/bat-go/wallet"
1313
"github.com/brave-intl/bat-go/wallet/provider"
1414
log "github.com/sirupsen/logrus"
@@ -20,7 +20,7 @@ var limit = flag.Int("limit", 50, "limit number of transactions returned")
2020
var walletProvider = flag.String("provider", "uphold", "provider for the source wallet")
2121

2222
func main() {
23-
log.SetFormatter(&utils.CliFormatter{})
23+
log.SetFormatter(&formatters.CliFormatter{})
2424

2525
flag.Usage = func() {
2626
log.Printf("A helper for fetching transaction history.\n\n")

bin/transfer-funds/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"flag"
66
"os"
77

8-
"github.com/brave-intl/bat-go/utils"
98
"github.com/brave-intl/bat-go/utils/altcurrency"
9+
"github.com/brave-intl/bat-go/utils/formatters"
1010
"github.com/brave-intl/bat-go/utils/httpsignature"
1111
"github.com/brave-intl/bat-go/utils/passphrase"
12+
"github.com/brave-intl/bat-go/utils/prompt"
1213
"github.com/brave-intl/bat-go/wallet"
1314
"github.com/brave-intl/bat-go/wallet/provider"
1415
"github.com/brave-intl/bat-go/wallet/provider/uphold"
@@ -27,7 +28,7 @@ var verbose = flag.Bool("v", false, "verbose output")
2728
var walletProvider = flag.String("provider", "uphold", "provider for the source wallet")
2829

2930
func main() {
30-
log.SetFormatter(&utils.CliFormatter{})
31+
log.SetFormatter(&formatters.CliFormatter{})
3132

3233
flag.Usage = func() {
3334
log.Printf("A utility for transferring funds.\n\n")
@@ -135,7 +136,7 @@ func main() {
135136
log.Printf("Will transfer %s %s from %s to %s\n", altc.FromProbi(valueProbi).String(), *currency, *from, *to)
136137

137138
log.Printf("Continue? ")
138-
resp, err := utils.PromptBool()
139+
resp, err := prompt.Bool()
139140
if err != nil {
140141
log.Fatalln(err)
141142
}
@@ -158,7 +159,7 @@ func main() {
158159
}
159160

160161
log.Printf("Confirmation did not appear to go through, retry? ")
161-
resp, err = utils.PromptBool()
162+
resp, err = prompt.Bool()
162163
if err != nil {
163164
log.Fatalln(err)
164165
}

bin/vault-init/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"os/user"
1212
"path"
1313

14-
"github.com/brave-intl/bat-go/utils"
14+
"github.com/brave-intl/bat-go/utils/closers"
1515
"github.com/brave-intl/bat-go/utils/vaultsigner"
1616
"github.com/hashicorp/vault/api"
1717
"golang.org/x/crypto/openpgp"
@@ -53,7 +53,7 @@ func main() {
5353
if err != nil {
5454
log.Fatalln(err)
5555
}
56-
defer utils.PanicCloser(f)
56+
defer closers.Panic(f)
5757

5858
// Vault only accepts keys in binary format, so we normalize the format
5959
var entity openpgp.EntityList

controllers/grants.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
"github.com/brave-intl/bat-go/datastore"
1212
"github.com/brave-intl/bat-go/grant"
1313
"github.com/brave-intl/bat-go/middleware"
14-
"github.com/brave-intl/bat-go/utils"
14+
"github.com/brave-intl/bat-go/utils/closers"
15+
"github.com/brave-intl/bat-go/utils/handlers"
1516
"github.com/garyburd/redigo/redis"
1617
raven "github.com/getsentry/raven-go"
1718
"github.com/go-chi/chi"
@@ -29,36 +30,36 @@ func GrantsRouter() chi.Router {
2930
if err != nil {
3031
panic("THROTTLE_GRANT_REQUESTS was provided but not a valid number")
3132
}
32-
r.Method("POST", "/", chiware.Throttle(int(throttle))(middleware.InstrumentHandler("RedeemGrants", utils.AppHandler(RedeemGrants))))
33+
r.Method("POST", "/", chiware.Throttle(int(throttle))(middleware.InstrumentHandler("RedeemGrants", handlers.AppHandler(RedeemGrants))))
3334
} else {
34-
r.Method("POST", "/", middleware.InstrumentHandler("RedeemGrants", utils.AppHandler(RedeemGrants)))
35+
r.Method("POST", "/", middleware.InstrumentHandler("RedeemGrants", handlers.AppHandler(RedeemGrants)))
3536
}
36-
r.Method("PUT", "/{grantId}", middleware.InstrumentHandler("ClaimGrant", utils.AppHandler(ClaimGrant)))
37+
r.Method("PUT", "/{grantId}", middleware.InstrumentHandler("ClaimGrant", handlers.AppHandler(ClaimGrant)))
3738
return r
3839
}
3940

4041
// ClaimGrant is the handler for claiming grants
41-
func ClaimGrant(w http.ResponseWriter, r *http.Request) *utils.AppError {
42-
defer utils.PanicCloser(r.Body)
42+
func ClaimGrant(w http.ResponseWriter, r *http.Request) *handlers.AppError {
43+
defer closers.Panic(r.Body)
4344

4445
body, err := ioutil.ReadAll(r.Body)
4546
if err != nil {
46-
return utils.WrapError("Error reading body", err)
47+
return handlers.WrapError("Error reading body", err)
4748
}
4849

4950
var req grant.ClaimGrantRequest
5051
err = json.Unmarshal(body, &req)
5152
if err != nil {
52-
return utils.WrapError("Error unmarshalling body", err)
53+
return handlers.WrapError("Error unmarshalling body", err)
5354
}
5455
_, err = govalidator.ValidateStruct(req)
5556
if err != nil {
56-
return utils.WrapValidationError(err)
57+
return handlers.WrapValidationError(err)
5758
}
5859

5960
if grantID := chi.URLParam(r, "grantId"); grantID != "" {
6061
if !govalidator.IsUUIDv4(grantID) {
61-
return &utils.AppError{
62+
return &handlers.AppError{
6263
Message: "Error validating request url parameter",
6364
Code: http.StatusBadRequest,
6465
Data: map[string]interface{}{
@@ -72,7 +73,7 @@ func ClaimGrant(w http.ResponseWriter, r *http.Request) *utils.AppError {
7273
err = req.Claim(r.Context(), grantID)
7374
if err != nil {
7475
// FIXME not all errors are 4xx
75-
return utils.WrapError("Error claiming grant", err)
76+
return handlers.WrapError("Error claiming grant", err)
7677
}
7778
}
7879

@@ -90,31 +91,31 @@ func ClaimGrant(w http.ResponseWriter, r *http.Request) *utils.AppError {
9091
}
9192

9293
// RedeemGrants is the handler for redeeming one or more grants
93-
func RedeemGrants(w http.ResponseWriter, r *http.Request) *utils.AppError {
94-
defer utils.PanicCloser(r.Body)
94+
func RedeemGrants(w http.ResponseWriter, r *http.Request) *handlers.AppError {
95+
defer closers.Panic(r.Body)
9596

9697
body, err := ioutil.ReadAll(r.Body)
9798
if err != nil {
98-
return utils.WrapError("Error reading body", err)
99+
return handlers.WrapError("Error reading body", err)
99100
}
100101

101102
var req grant.RedeemGrantsRequest
102103
err = json.Unmarshal(body, &req)
103104
if err != nil {
104-
return utils.WrapError("Error unmarshalling body", err)
105+
return handlers.WrapError("Error unmarshalling body", err)
105106
}
106107
_, err = govalidator.ValidateStruct(req)
107108
if err != nil {
108-
return utils.WrapValidationError(err)
109+
return handlers.WrapValidationError(err)
109110
}
110111

111112
redeemedIDs, err := grant.GetRedeemedIDs(r.Context(), req.Grants)
112113
if err != nil {
113-
return utils.WrapError("Error checking grant redemption status", err)
114+
return handlers.WrapError("Error checking grant redemption status", err)
114115
}
115116

116117
if len(redeemedIDs) > 0 {
117-
return &utils.AppError{
118+
return &handlers.AppError{
118119
Message: "One or more grants have already been redeemed",
119120
Code: http.StatusGone,
120121
Data: map[string]interface{}{"redeemedIDs": redeemedIDs},
@@ -124,7 +125,7 @@ func RedeemGrants(w http.ResponseWriter, r *http.Request) *utils.AppError {
124125
txInfo, err := req.Redeem(r.Context())
125126
if err != nil {
126127
// FIXME not all errors are 4xx
127-
return utils.WrapError("Error redeeming grant", err)
128+
return handlers.WrapError("Error redeeming grant", err)
128129
}
129130

130131
w.WriteHeader(http.StatusOK)

grant/claim.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77

88
"github.com/brave-intl/bat-go/datastore"
9-
"github.com/brave-intl/bat-go/utils"
9+
"github.com/brave-intl/bat-go/utils/closers"
1010
"github.com/brave-intl/bat-go/wallet"
1111
"github.com/pressly/lg"
1212
"github.com/prometheus/client_golang/prometheus"
@@ -30,7 +30,7 @@ func (req *ClaimGrantRequest) Claim(ctx context.Context, grantID string) error {
3030
if err != nil {
3131
return err
3232
}
33-
defer utils.PanicCloser(kvDatastore)
33+
defer closers.Panic(kvDatastore)
3434

3535
_, err = kvDatastore.Set(
3636
fmt.Sprintf(claimKeyFormat, grantID),

grant/redeem.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"time"
99

1010
"github.com/brave-intl/bat-go/datastore"
11-
"github.com/brave-intl/bat-go/utils"
1211
"github.com/brave-intl/bat-go/utils/altcurrency"
12+
"github.com/brave-intl/bat-go/utils/closers"
1313
"github.com/brave-intl/bat-go/wallet"
1414
"github.com/brave-intl/bat-go/wallet/provider"
1515
raven "github.com/getsentry/raven-go"
@@ -137,7 +137,7 @@ func (req *RedeemGrantsRequest) VerifyAndConsume(ctx context.Context) (*wallet.T
137137
if err != nil {
138138
return nil, err
139139
}
140-
defer utils.PanicCloser(kvDatastore)
140+
defer closers.Panic(kvDatastore)
141141
// 6. Iterate through grants and check that:
142142
for _, grant := range grants {
143143
claimedID, err := GetClaimantID(kvDatastore, grant.GrantID.String())
@@ -163,12 +163,12 @@ func (req *RedeemGrantsRequest) VerifyAndConsume(ctx context.Context) (*wallet.T
163163
if err != nil {
164164
return nil, err
165165
}
166-
defer utils.PanicCloser(redeemedGrants)
166+
defer closers.Panic(redeemedGrants)
167167
redeemedWallets, err := GetPromotionWalletsDatastore(ctx, grant.PromotionID.String())
168168
if err != nil {
169169
return nil, err
170170
}
171-
defer utils.PanicCloser(redeemedWallets)
171+
defer closers.Panic(redeemedWallets)
172172

173173
result, err := redeemedGrants.Add(grant.GrantID.String())
174174
if err != nil {
@@ -221,7 +221,7 @@ func GetRedeemedIDs(ctx context.Context, Grants []string) ([]string, error) {
221221
if err != nil {
222222
return nil, err
223223
}
224-
defer utils.PanicCloser(redeemedGrants)
224+
defer closers.Panic(redeemedGrants)
225225

226226
grantRedeemed, err := redeemedGrants.Contains(grantID)
227227
if err != nil {

grant/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"strings"
88

99
"github.com/brave-intl/bat-go/datastore"
10-
"github.com/brave-intl/bat-go/utils"
1110
"github.com/brave-intl/bat-go/utils/altcurrency"
11+
"github.com/brave-intl/bat-go/utils/closers"
1212
"github.com/brave-intl/bat-go/utils/httpsignature"
1313
"github.com/brave-intl/bat-go/wallet"
1414
"github.com/brave-intl/bat-go/wallet/provider/uphold"
@@ -144,7 +144,7 @@ func (gs *grantService) Describe(ch chan<- *prometheus.Desc) {
144144
// We implement this and the Describe function to fulfill the prometheus.Collector interface
145145
func (gs *grantService) Collect(ch chan<- prometheus.Metric) {
146146
conn := gs.pool.Get()
147-
defer utils.PanicCloser(conn)
147+
defer closers.Panic(conn)
148148

149149
kv := datastore.GetRedisKv(&conn)
150150
ogCount, err := kv.Count("grant:*")

middleware/request_logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
"runtime/debug"
3838
"time"
3939

40-
"github.com/brave-intl/bat-go/utils"
40+
"github.com/brave-intl/bat-go/utils/handlers"
4141
"github.com/getsentry/raven-go"
4242
"github.com/go-chi/chi/middleware"
4343
"github.com/pressly/lg"
@@ -77,7 +77,7 @@ func RequestLogger(logger *logrus.Logger) func(next http.Handler) http.Handler {
7777
)
7878
raven.Capture(packet, nil)
7979

80-
utils.AppError{
80+
handlers.AppError{
8181
Message: http.StatusText(http.StatusInternalServerError),
8282
Code: http.StatusInternalServerError,
8383
}.ServeHTTP(w, r)

utils/closer.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)