Skip to content

Commit

Permalink
Export malformed request struct
Browse files Browse the repository at this point in the history
  • Loading branch information
weeco committed Dec 19, 2020
1 parent d7141df commit df279ba
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions rest/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Decode(w http.ResponseWriter, r *http.Request, dst interface{}) error {
value, _ := header.ParseValueAndParams(r.Header, "Content-Type")
if value != "application/json" {
msg := "Content-Type header is not application/json"
return &malformedRequest{status: http.StatusUnsupportedMediaType, msg: msg}
return &MalformedRequest{status: http.StatusUnsupportedMediaType, msg: msg}
}
}

Expand All @@ -39,28 +39,28 @@ func Decode(w http.ResponseWriter, r *http.Request, dst interface{}) error {
switch {
case errors.As(err, &syntaxError):
msg := fmt.Sprintf("Request body contains badly-formed JSON (at position %d)", syntaxError.Offset)
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
return &MalformedRequest{status: http.StatusBadRequest, msg: msg}

case errors.Is(err, io.ErrUnexpectedEOF):
msg := fmt.Sprintf("Request body contains badly-formed JSON")
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
return &MalformedRequest{status: http.StatusBadRequest, msg: msg}

case errors.As(err, &unmarshalTypeError):
msg := fmt.Sprintf("Request body contains an invalid value for the %q field (at position %d)", unmarshalTypeError.Field, unmarshalTypeError.Offset)
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
return &MalformedRequest{status: http.StatusBadRequest, msg: msg}

case strings.HasPrefix(err.Error(), "json: unknown field "):
fieldName := strings.TrimPrefix(err.Error(), "json: unknown field ")
msg := fmt.Sprintf("Request body contains unknown field %s", fieldName)
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
return &MalformedRequest{status: http.StatusBadRequest, msg: msg}

case errors.Is(err, io.EOF):
msg := "Request body must not be empty"
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
return &MalformedRequest{status: http.StatusBadRequest, msg: msg}

case err.Error() == "http: request body too large":
msg := "Request body must not be larger than 1MB"
return &malformedRequest{status: http.StatusRequestEntityTooLarge, msg: msg}
return &MalformedRequest{status: http.StatusRequestEntityTooLarge, msg: msg}

default:
return err
Expand All @@ -70,26 +70,26 @@ func Decode(w http.ResponseWriter, r *http.Request, dst interface{}) error {
err = dec.Decode(&struct{}{})
if err != io.EOF {
msg := "Request body must only contain a single JSON object"
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
return &MalformedRequest{status: http.StatusBadRequest, msg: msg}
}

if valid, ok := dst.(interface {
OK() error
}); ok {
err = valid.OK()
if err != nil {
return &malformedRequest{status: http.StatusBadRequest, msg: fmt.Sprintf("validating the decoded object failed: %v", err.Error())}
return &MalformedRequest{status: http.StatusBadRequest, msg: fmt.Sprintf("validating the decoded object failed: %v", err.Error())}
}
}

return nil
}

type malformedRequest struct {
type MalformedRequest struct {
status int
msg string
}

func (mr *malformedRequest) Error() string {
func (mr *MalformedRequest) Error() string {
return mr.msg
}

0 comments on commit df279ba

Please sign in to comment.