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)
| 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. |
| 262 | func 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). |
| 275 | func (f EncodingFunc) Decode(v any) error { return f(v) } |