Skip to content

Commit

Permalink
Handle unsupported request content type (#3513)
Browse files Browse the repository at this point in the history
* Handle unsupported request content type

Return 415 Unsupported Media Type when the request decoder does not support the request content type.

* Make linter happy

* Do not wrap decode errors that are already service errors

This makes it possible for the decoding code to return proper HTTP statuses depending on the name of error.
As of this PR, this is only used to return 415 "Unsupported Media Type" when an unsupported content type is used to encode the request payload.

* Refactor slightly to group error definitions
  • Loading branch information
raphael committed May 1, 2024
1 parent 026621f commit f11a242
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 50 deletions.
8 changes: 8 additions & 0 deletions http/codegen/templates/request_decoder.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ func {{ .RequestDecoder }}(mux goahttp.Muxer, decoder func(*http.Request) goahtt
{{- if .MultipartRequestDecoder }}
var payload {{ .Payload.Ref }}
if err := decoder(r).Decode(&payload); err != nil {
var gerr *goa.ServiceError
if errors.As(err, &gerr) {
return nil, gerr
}
return nil, goa.DecodePayloadError(err.Error())
}
{{- else if .Payload.Request.ServerBody }}
Expand All @@ -22,6 +26,10 @@ func {{ .RequestDecoder }}(mux goahttp.Muxer, decoder func(*http.Request) goahtt
err = nil
} else {
{{- end }}
var gerr *goa.ServiceError
if errors.As(err, &gerr) {
return nil, gerr
}
return nil, goa.DecodePayloadError(err.Error())
{{- if not .Payload.Request.MustHaveBody }}
}
Expand Down
Loading

0 comments on commit f11a242

Please sign in to comment.