MCPcopy Create free account
hub / github.com/goadesign/goa / ErrorEncoder

Function ErrorEncoder

http/encoding.go:262–272  ·  view source on GitHub ↗

ErrorEncoder returns an encoder that encodes errors returned by service methods. The default encoder checks whether the error is a goa ServiceError struct and if so uses the error temporary and timeout fields to infer a proper HTTP status code and marshals the error struct to the body using the prov

(encoder func(context.Context, http.ResponseWriter) Encoder, formatter func(ctx context.Context, err error) Statuser)

Source from the content-addressed store, hash-verified

260// encoded as a permanent internal server error. This behavior as well as the
261// shape of the response can be overridden by providing a non-nil formatter.
262func ErrorEncoder(encoder func(context.Context, http.ResponseWriter) Encoder, formatter func(ctx context.Context, err error) Statuser) func(context.Context, http.ResponseWriter, error) error {
263 return func(ctx context.Context, w http.ResponseWriter, err error) error {
264 enc := encoder(ctx, w)
265 if formatter == nil {
266 formatter = NewErrorResponse
267 }
268 resp := formatter(ctx, err)
269 w.WriteHeader(resp.StatusCode())
270 return enc.Encode(resp)
271 }
272}
273
274// Decode implements the Decoder interface. It simply calls f(v).
275func (f EncodingFunc) Decode(v any) error { return f(v) }

Callers 1

TestUnsupportedDecoderFunction · 0.85

Calls 3

StatusCodeMethod · 0.65
EncodeMethod · 0.65
WriteHeaderMethod · 0.45

Tested by 1

TestUnsupportedDecoderFunction · 0.68