Skip to content

Commit 7511a79

Browse files
committed
Name all context extracting functions consistently
Response -> ContextResponse Request -> ContextRequest RequestService -> ContextService RequestLogger -> ContextLogger
1 parent b3ce4da commit 7511a79

11 files changed

+84
-83
lines changed

context.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,32 @@ func UseLogger(ctx context.Context, logger Logger) context.Context {
6666
return context.WithValue(ctx, logKey, logger)
6767
}
6868

69-
// Request gives access to the underlying HTTP request.
70-
func Request(ctx context.Context) *RequestData {
69+
// ContextRequest gives access to the underlying HTTP request.
70+
func ContextRequest(ctx context.Context) *RequestData {
7171
if r := ctx.Value(reqKey); r != nil {
7272
return r.(*RequestData)
7373
}
7474
return nil
7575
}
7676

77-
// Response gives access to the underlying HTTP response.
78-
func Response(ctx context.Context) *ResponseData {
77+
// ContextResponse gives access to the underlying HTTP response.
78+
func ContextResponse(ctx context.Context) *ResponseData {
7979
if r := ctx.Value(respKey); r != nil {
8080
return r.(*ResponseData)
8181
}
8282
return nil
8383
}
8484

85-
// RequestService returns the service tageted by the request with the given context.
86-
func RequestService(ctx context.Context) *Service {
85+
// ContextService returns the service tageted by the request with the given context.
86+
func ContextService(ctx context.Context) *Service {
8787
if r := ctx.Value(serviceKey); r != nil {
8888
return r.(*Service)
8989
}
9090
return nil
9191
}
9292

93-
// RequestLogger returns the logger used by the request context.
94-
func RequestLogger(ctx context.Context) Logger {
93+
// ContextLogger returns the logger used by the request context.
94+
func ContextLogger(ctx context.Context) Logger {
9595
if v := ctx.Value(logKey); v != nil {
9696
return v.(Logger)
9797
}
@@ -115,7 +115,7 @@ func (r *ResponseData) Written() bool {
115115
// encoders. It uses the default service encoder if no match is found.
116116
func (r *ResponseData) Send(ctx context.Context, code int, body interface{}) error {
117117
r.WriteHeader(code)
118-
return RequestService(ctx).EncodeResponse(ctx, body)
118+
return ContextService(ctx).EncodeResponse(ctx, body)
119119
}
120120

121121
// BadRequest sends a HTTP response with status code 400 and the given error as body.

context_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var _ = Describe("ResponseData", func() {
2424
rw = &TestResponseWriter{Status: 42}
2525
params = url.Values{"query": []string{"value"}}
2626
ctx := goa.NewContext(context.Background(), rw, req, params)
27-
data = goa.Response(ctx)
27+
data = goa.ContextResponse(ctx)
2828
})
2929

3030
Context("SwitchWriter", func() {

encoding.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (p *decoderPool) Put(d Decoder) {
180180
// `Accept` header and writes it to the http.ResponseWriter
181181
func (service *Service) EncodeResponse(ctx context.Context, v interface{}) error {
182182
now := time.Now()
183-
accept := Request(ctx).Header.Get("Accept")
183+
accept := ContextRequest(ctx).Header.Get("Accept")
184184
if accept == "" {
185185
accept = "*/*"
186186
}
@@ -201,7 +201,7 @@ func (service *Service) EncodeResponse(ctx context.Context, v interface{}) error
201201
}
202202

203203
// the encoderPool will handle whether or not a pool is actually in use
204-
encoder := p.Get(Response(ctx))
204+
encoder := p.Get(ContextResponse(ctx))
205205
if err := encoder.Encode(v); err != nil {
206206
return err
207207
}

error.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
// Package goa standardizes on structured error responses: a request that fails because of
2-
// invalid input or unexpected condition produces a response that contains one or more structured
3-
// error(s). Each error object has three keys: a id (number), a title and a message. The title
4-
// for a given id is always the same, the intent is to provide a human friendly categorization.
5-
// The message is specific to the error occurrence and provides additional details that often
6-
// include contextual information (name of parameters etc.).
2+
// invalid input or unexpected condition produces a response that contains a structured error.
3+
// Error objects has four keys: a code, a status, a detail and metadata. The code defines the class
4+
// of error (e.g. "invalid_parameter_type") and the status is the corresponding HTTP status
5+
// (e.g. 400). The detail is specific to the error occurrence. The medata provides additional
6+
// values that provide contextual information (name of parameters etc.).
77
//
8-
// The basic data structure backing errors is TypedError which simply contains the id and message.
9-
// Multiple errors (not just TypedError instances) can be encapsulated in a HTTPError. Both
10-
// TypedError and HTTPError implement the error interface, the Error methods return valid JSON
11-
// that can be written directly to a response body.
8+
// The basic data structure backing errors is Error. Instances of Error can be created via Error
9+
// Class functions. See http://goa.design/implement/error_handling.html
1210
//
1311
// The code generated by goagen calls the helper functions exposed in this file when it encounters
1412
// invalid data (wrong type, validation errors etc.) such as InvalidParamTypeError,
15-
// InvalidAttributeTypeError etc. These methods take and return an error which is a HTTPError that
16-
// gets built over time. The final HTTPError object then gets serialized into the response and sent
17-
// back to the client. The response status code is inferred from the type wrapping the error object:
18-
// a BadRequestError produces a 400 status code while any other error produce a 500. This behavior
19-
// can be overridden by setting a custom ErrorHandler in the application.
13+
// InvalidAttributeTypeError etc. These methods return errors that get merged with any previously
14+
// encountered error via the Error Merge method.
15+
//
16+
// goa includes an error handler middleware that takes care of mapping back any error returned by
17+
// previously called middleware or action handler into HTTP responses. If the error is an instance
18+
// of Error then the corresponding content including the HTTP status is used otherwise an internal
19+
// error is returned. Errors that bubble up all the way to the top (i.e. not handled by the error
20+
// middleware) also generate an internal error response.
2021
package goa
2122

2223
import (

goagen/codegen/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func GoTypeDesc(t design.DataType, upper bool) string {
269269
// or the empty string if there isn't one.
270270
func GoaMediaTypeName(mt *design.MediaTypeDefinition) string {
271271
if mt == design.ErrorMedia {
272-
return "*goa.HTTPError"
272+
return "*goa.Error"
273273
}
274274
return ""
275275
}

goagen/gen_app/generator_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ type GetWidgetContext struct {
360360
// context used by the Widget controller get action.
361361
func NewGetWidgetContext(ctx context.Context) (*GetWidgetContext, error) {
362362
var err *goa.Error
363-
req := goa.Request(ctx)
364-
rctx := GetWidgetContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
363+
req := goa.ContextRequest(ctx)
364+
rctx := GetWidgetContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
365365
rawID := req.Params.Get("id")
366366
if rawID != "" {
367367
rctx.ID = rawID
@@ -479,7 +479,7 @@ func MountWidgetController(service *goa.Service, ctrl WidgetController) {
479479
if err != nil {
480480
return err
481481
}
482-
if rawPayload := goa.Request(ctx).Payload; rawPayload != nil {
482+
if rawPayload := goa.ContextRequest(ctx).Payload; rawPayload != nil {
483483
rctx.Payload = rawPayload.(Collection)
484484
}
485485
return ctrl.Get(rctx)
@@ -491,10 +491,10 @@ func MountWidgetController(service *goa.Service, ctrl WidgetController) {
491491
// unmarshalGetWidgetPayload unmarshals the request body into the context request data Payload field.
492492
func unmarshalGetWidgetPayload(ctx context.Context, req *http.Request) error {
493493
var payload Collection
494-
if err := goa.RequestService(ctx).DecodeRequest(req, &payload); err != nil {
494+
if err := goa.ContextService(ctx).DecodeRequest(req, &payload); err != nil {
495495
return err
496496
}
497-
goa.Request(ctx).Payload = payload
497+
goa.ContextRequest(ctx).Payload = payload
498498
return nil
499499
}
500500
`

goagen/gen_app/writers.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ type {{ .Name }} struct {
460460
// context used by the {{ .ResourceName }} controller {{ .ActionName }} action.
461461
func New{{ .Name }}(ctx context.Context) (*{{ .Name }}, error) {
462462
var err *goa.Error
463-
req := goa.Request(ctx)
464-
rctx := {{ .Name }}{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
463+
req := goa.ContextRequest(ctx)
464+
rctx := {{ .Name }}{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
465465
{{ if .Headers }}{{ $headers := .Headers }}{{ range $name, $att := $headers.Type.ToObject }} raw{{ goify $name true }} := req.Header.Get("{{ $name }}")
466466
{{ if $headers.IsRequired $name }} if raw{{ goify $name true }} == "" {
467467
err = err.Merge(goa.MissingHeaderError("{{ $name }}"))
@@ -579,7 +579,7 @@ func Mount{{ .Resource }}Controller(service *goa.Service, ctrl {{ .Resource }}Co
579579
if err != nil {
580580
return err
581581
}
582-
{{ if .Payload }}if rawPayload := goa.Request(ctx).Payload; rawPayload != nil {
582+
{{ if .Payload }}if rawPayload := goa.ContextRequest(ctx).Payload; rawPayload != nil {
583583
rctx.Payload = rawPayload.({{ gotyperef .Payload nil 1 }})
584584
}
585585
{{ end }} return ctrl.{{ .Name }}(rctx)
@@ -626,13 +626,13 @@ func handle{{ .Resource }}Origin(h goa.Handler) goa.Handler {
626626
// {{ .Unmarshal }} unmarshals the request body into the context request data Payload field.
627627
func {{ .Unmarshal }}(ctx context.Context, req *http.Request) error {
628628
var payload {{ gotypename .Payload nil 1 }}
629-
if err := goa.RequestService(ctx).DecodeRequest(req, &payload); err != nil {
629+
if err := goa.ContextService(ctx).DecodeRequest(req, &payload); err != nil {
630630
return err
631631
}{{ $validation := recursiveValidate .Payload.AttributeDefinition false false "payload" "raw" 1 }}{{ if $validation }}
632632
if err := payload.Validate(); err != nil {
633633
return err
634634
}{{ end }}
635-
goa.Request(ctx).Payload = {{ if .Payload.IsObject }}&{{ end }}payload
635+
goa.ContextRequest(ctx).Payload = {{ if .Payload.IsObject }}&{{ end }}payload
636636
return nil
637637
}
638638
{{ end }}

goagen/gen_app/writers_test.go

+24-24
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,8 @@ type ListBottleContext struct {
723723
emptyContextFactory = `
724724
func NewListBottleContext(ctx context.Context) (*ListBottleContext, error) {
725725
var err *goa.Error
726-
req := goa.Request(ctx)
727-
rctx := ListBottleContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
726+
req := goa.ContextRequest(ctx)
727+
rctx := ListBottleContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
728728
return &rctx, err
729729
}
730730
`
@@ -741,8 +741,8 @@ type ListBottleContext struct {
741741
intContextFactory = `
742742
func NewListBottleContext(ctx context.Context) (*ListBottleContext, error) {
743743
var err *goa.Error
744-
req := goa.Request(ctx)
745-
rctx := ListBottleContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
744+
req := goa.ContextRequest(ctx)
745+
rctx := ListBottleContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
746746
rawParam := req.Params.Get("param")
747747
if rawParam != "" {
748748
if param, err2 := strconv.Atoi(rawParam); err2 == nil {
@@ -769,8 +769,8 @@ type ListBottleContext struct {
769769
strContextFactory = `
770770
func NewListBottleContext(ctx context.Context) (*ListBottleContext, error) {
771771
var err *goa.Error
772-
req := goa.Request(ctx)
773-
rctx := ListBottleContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
772+
req := goa.ContextRequest(ctx)
773+
rctx := ListBottleContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
774774
rawParam := req.Params.Get("param")
775775
if rawParam != "" {
776776
rctx.Param = &rawParam
@@ -791,8 +791,8 @@ type ListBottleContext struct {
791791
numContextFactory = `
792792
func NewListBottleContext(ctx context.Context) (*ListBottleContext, error) {
793793
var err *goa.Error
794-
req := goa.Request(ctx)
795-
rctx := ListBottleContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
794+
req := goa.ContextRequest(ctx)
795+
rctx := ListBottleContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
796796
rawParam := req.Params.Get("param")
797797
if rawParam != "" {
798798
if param, err2 := strconv.ParseFloat(rawParam, 64); err2 == nil {
@@ -817,8 +817,8 @@ type ListBottleContext struct {
817817
boolContextFactory = `
818818
func NewListBottleContext(ctx context.Context) (*ListBottleContext, error) {
819819
var err *goa.Error
820-
req := goa.Request(ctx)
821-
rctx := ListBottleContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
820+
req := goa.ContextRequest(ctx)
821+
rctx := ListBottleContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
822822
rawParam := req.Params.Get("param")
823823
if rawParam != "" {
824824
if param, err2 := strconv.ParseBool(rawParam); err2 == nil {
@@ -844,8 +844,8 @@ type ListBottleContext struct {
844844
arrayContextFactory = `
845845
func NewListBottleContext(ctx context.Context) (*ListBottleContext, error) {
846846
var err *goa.Error
847-
req := goa.Request(ctx)
848-
rctx := ListBottleContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
847+
req := goa.ContextRequest(ctx)
848+
rctx := ListBottleContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
849849
rawParam := req.Params.Get("param")
850850
if rawParam != "" {
851851
elemsParam := strings.Split(rawParam, ",")
@@ -867,8 +867,8 @@ type ListBottleContext struct {
867867
intArrayContextFactory = `
868868
func NewListBottleContext(ctx context.Context) (*ListBottleContext, error) {
869869
var err *goa.Error
870-
req := goa.Request(ctx)
871-
rctx := ListBottleContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
870+
req := goa.ContextRequest(ctx)
871+
rctx := ListBottleContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
872872
rawParam := req.Params.Get("param")
873873
if rawParam != "" {
874874
elemsParam := strings.Split(rawParam, ",")
@@ -898,8 +898,8 @@ type ListBottleContext struct {
898898
resContextFactory = `
899899
func NewListBottleContext(ctx context.Context) (*ListBottleContext, error) {
900900
var err *goa.Error
901-
req := goa.Request(ctx)
902-
rctx := ListBottleContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
901+
req := goa.ContextRequest(ctx)
902+
rctx := ListBottleContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
903903
rawInt := req.Params.Get("int")
904904
if rawInt != "" {
905905
if int_, err2 := strconv.Atoi(rawInt); err2 == nil {
@@ -926,8 +926,8 @@ type ListBottleContext struct {
926926
requiredContextFactory = `
927927
func NewListBottleContext(ctx context.Context) (*ListBottleContext, error) {
928928
var err *goa.Error
929-
req := goa.Request(ctx)
930-
rctx := ListBottleContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
929+
req := goa.ContextRequest(ctx)
930+
rctx := ListBottleContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
931931
rawInt := req.Params.Get("int")
932932
if rawInt == "" {
933933
err = err.Merge(goa.MissingParamError("int"))
@@ -954,8 +954,8 @@ type ListBottleContext struct {
954954
payloadContextFactory = `
955955
func NewListBottleContext(ctx context.Context) (*ListBottleContext, error) {
956956
var err *goa.Error
957-
req := goa.Request(ctx)
958-
rctx := ListBottleContext{Context: ctx, ResponseData: goa.Response(ctx), RequestData: req}
957+
req := goa.ContextRequest(ctx)
958+
rctx := ListBottleContext{Context: ctx, ResponseData: goa.ContextResponse(ctx), RequestData: req}
959959
return &rctx, err
960960
}
961961
`
@@ -971,23 +971,23 @@ type ListBottleContext struct {
971971
payloadObjUnmarshal = `
972972
func unmarshalListBottlePayload(ctx context.Context, req *http.Request) error {
973973
var payload ListBottlePayload
974-
if err := goa.RequestService(ctx).DecodeRequest(req, &payload); err != nil {
974+
if err := goa.ContextService(ctx).DecodeRequest(req, &payload); err != nil {
975975
return err
976976
}
977977
if err := payload.Validate(); err != nil {
978978
return err
979979
}
980-
goa.Request(ctx).Payload = &payload
980+
goa.ContextRequest(ctx).Payload = &payload
981981
return nil
982982
}
983983
`
984984
payloadNoValidationsObjUnmarshal = `
985985
func unmarshalListBottlePayload(ctx context.Context, req *http.Request) error {
986986
var payload ListBottlePayload
987-
if err := goa.RequestService(ctx).DecodeRequest(req, &payload); err != nil {
987+
if err := goa.ContextService(ctx).DecodeRequest(req, &payload); err != nil {
988988
return err
989989
}
990-
goa.Request(ctx).Payload = &payload
990+
goa.ContextRequest(ctx).Payload = &payload
991991
return nil
992992
}
993993
`

middleware_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ var _ = Describe("NewMiddleware", func() {
6262
Ω(err).ShouldNot(HaveOccurred())
6363
rw = new(TestResponseWriter)
6464
ctx = goa.NewContext(ctrl.Context, rw, req, nil)
65-
Ω(goa.Response(ctx).Status).Should(Equal(0))
65+
Ω(goa.ContextResponse(ctx).Status).Should(Equal(0))
6666
})
6767

6868
Context("using a goa handler", func() {
6969
BeforeEach(func() {
7070
var goaHandler goa.Handler = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
71-
goa.Response(ctx).Send(ctx, 200, "ok")
71+
goa.ContextResponse(ctx).Send(ctx, 200, "ok")
7272
return nil
7373
}
7474
input = goaHandler
@@ -78,14 +78,14 @@ var _ = Describe("NewMiddleware", func() {
7878
Ω(mErr).ShouldNot(HaveOccurred())
7979
h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { return nil }
8080
Ω(middleware(h)(ctx, rw, req)).ShouldNot(HaveOccurred())
81-
Ω(goa.Response(ctx).Status).Should(Equal(200))
81+
Ω(goa.ContextResponse(ctx).Status).Should(Equal(200))
8282
})
8383
})
8484

8585
Context("using a goa handler func", func() {
8686
BeforeEach(func() {
8787
input = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
88-
goa.Response(ctx).Send(ctx, 200, "ok")
88+
goa.ContextResponse(ctx).Send(ctx, 200, "ok")
8989
return nil
9090
}
9191
})
@@ -94,7 +94,7 @@ var _ = Describe("NewMiddleware", func() {
9494
Ω(mErr).ShouldNot(HaveOccurred())
9595
h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { return nil }
9696
Ω(middleware(h)(ctx, rw, req)).ShouldNot(HaveOccurred())
97-
Ω(goa.Response(ctx).Status).Should(Equal(200))
97+
Ω(goa.ContextResponse(ctx).Status).Should(Equal(200))
9898
})
9999
})
100100

@@ -106,11 +106,11 @@ var _ = Describe("NewMiddleware", func() {
106106
It("wraps it in a middleware", func() {
107107
Ω(mErr).ShouldNot(HaveOccurred())
108108
h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
109-
goa.Response(ctx).Send(ctx, 200, "ok")
109+
goa.ContextResponse(ctx).Send(ctx, 200, "ok")
110110
return nil
111111
}
112112
Ω(middleware(h)(ctx, rw, req)).ShouldNot(HaveOccurred())
113-
Ω(goa.Response(ctx).Status).Should(Equal(200))
113+
Ω(goa.ContextResponse(ctx).Status).Should(Equal(200))
114114
})
115115
})
116116

0 commit comments

Comments
 (0)