func jsonStores(ctx context.Context, w http.ResponseWriter, r *http.Request) error { storeReader, _, err := store.FromContextReader(ctx) if err != nil { return err // default StatusInternalServerError } stores, err := storeReader.Stores() if err != nil { return ctxhttp.NewErrorFromErrors(http.StatusInternalServerError, err) } return httputil.NewPrinter(w, r).JSON(http.StatusOK, stores) }
func TestNewErrorFromErrors(t *testing.T) { tests := []struct { code int errs []error wantError string }{ {http.StatusBadGateway, nil, http.StatusText(http.StatusBadGateway)}, {http.StatusTeapot, []error{errors.New("No coffee pot"), errors.New("Not even a milk pot")}, "No coffee pot\nNot even a milk pot"}, {http.StatusConflict, []error{errgo.New("Now a coffee pot"), errgo.New("Not even close to a milk pot")}, "error_test.go"}, } for _, test := range tests { he := ctxhttp.NewErrorFromErrors(test.code, test.errs...) assert.Exactly(t, test.code, he.Code) assert.Contains(t, he.Error(), test.wantError) } }