Skip to content

Commit

Permalink
Export malformed request properties
Browse files Browse the repository at this point in the history
  • Loading branch information
weeco committed Dec 19, 2020
1 parent df279ba commit 027980a
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 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, Message: 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, Message: 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, Message: 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, Message: 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, Message: 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, Message: 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, Message: 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, Message: 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, Message: fmt.Sprintf("validating the decoded object failed: %v", err.Error())}
}
}

return nil
}

type MalformedRequest struct {
status int
msg string
Status int
Message string
}

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

0 comments on commit 027980a

Please sign in to comment.