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