Permalink
Browse files

Better handling of errors with coupon cache

  • Loading branch information...
biilmann committed Nov 15, 2017
1 parent 9084f48 commit 5e578e87126882182a418fe7dc3b15a53cdfeab4
Showing with 7 additions and 6 deletions.
  1. +4 −1 api/middleware.go
  2. +3 −5 context/context.go
View
@@ -111,7 +111,10 @@ func (api *API) loadInstanceConfig(w http.ResponseWriter, r *http.Request) (cont
func WithInstanceConfig(ctx context.Context, smtp conf.SMTPConfiguration, config *conf.Configuration, instanceID string) (context.Context, error) {
ctx = gcontext.WithInstanceID(ctx, instanceID)
ctx = gcontext.WithConfig(ctx, config)
ctx = gcontext.WithCoupons(ctx, config)
ctx, err := gcontext.WithCoupons(ctx, config)
if err != nil {
return nil, err
}
mailer := mailer.NewMailer(smtp, config)
ctx = gcontext.WithMailer(ctx, mailer)
View
@@ -54,14 +54,12 @@ func GetConfig(ctx context.Context) *conf.Configuration {
}
// WithCoupons adds the coupon cache to the context based on the site URL.
func WithCoupons(ctx context.Context, config *conf.Configuration) context.Context {
func WithCoupons(ctx context.Context, config *conf.Configuration) (context.Context, error) {
cache, err := coupons.NewCouponCacheFromURL(config)
if err != nil {
// We don't have access to the logger here, but it's better to say something than nothing
fmt.Printf("Error getting coupon cache %v", err)
return ctx
return nil, err
}
return context.WithValue(ctx, couponsKey, cache)
return context.WithValue(ctx, couponsKey, cache), nil
}
// GetCoupons reads the coupon cache from the context.

0 comments on commit 5e578e8

Please sign in to comment.