ducky/subscriptions

Paddy 2015-07-18 Parent:36e90e828dd0 Child:b063bc0a6e84

9:8eb19bcbf17d Go to Latest

ducky/subscriptions/api/context_helpers.go

Return errors from responses in client. When the client makes a request, non-200 responses _are not_ considered errors. So we need to check the response.Errors property, and if it has errors, _then_ we consider the request to have an error. To make this happen, we created an httpErrors type that fulfills the error interface and just wraps the response Errors property. Then callers can type-cast it and interrogate it.

History
paddy@4 1 package api
paddy@4 2
paddy@4 3 import (
paddy@4 4 "errors"
paddy@4 5
paddy@4 6 "code.secondbit.org/ducky/subscriptions.hg"
paddy@4 7
paddy@4 8 "golang.org/x/net/context"
paddy@4 9 )
paddy@4 10
paddy@4 11 const (
paddy@4 12 subscriptionStoreKey = "SubscriptionStore"
paddy@4 13 stripeKey = "Stripe"
paddy@4 14 )
paddy@4 15
paddy@4 16 var (
paddy@4 17 ErrSubscriptionStoreNotSet = errors.New("SubscriptionStore not set")
paddy@4 18 ErrStripeClientNotSet = errors.New("Stripe not set")
paddy@4 19 )
paddy@4 20
paddy@4 21 func getSubscriptionStore(c context.Context) (subscriptions.SubscriptionStore, error) {
paddy@4 22 store := c.Value(subscriptionStoreKey)
paddy@4 23 if store == nil {
paddy@4 24 return nil, ErrSubscriptionStoreNotSet
paddy@4 25 }
paddy@4 26 if s, ok := store.(subscriptions.SubscriptionStore); ok {
paddy@4 27 return s, nil
paddy@4 28 }
paddy@4 29 return nil, ErrSubscriptionStoreNotSet
paddy@4 30 }
paddy@4 31
paddy@4 32 func WithSubscriptionStore(store subscriptions.SubscriptionStore, c context.Context) context.Context {
paddy@4 33 return context.WithValue(c, subscriptionStoreKey, store)
paddy@4 34 }
paddy@4 35
paddy@4 36 func getStripeClient(c context.Context) (subscriptions.Stripe, error) {
paddy@4 37 stripe := c.Value(stripeKey)
paddy@4 38 if stripe == nil {
paddy@4 39 return subscriptions.Stripe{}, ErrStripeClientNotSet
paddy@4 40 }
paddy@4 41 if s, ok := stripe.(subscriptions.Stripe); ok {
paddy@4 42 return s, nil
paddy@4 43 }
paddy@4 44 return subscriptions.Stripe{}, ErrStripeClientNotSet
paddy@4 45 }
paddy@4 46
paddy@4 47 func WithStripeClient(stripe subscriptions.Stripe, c context.Context) context.Context {
paddy@4 48 return context.WithValue(c, stripeKey, stripe)
paddy@4 49 }