Skip to content

Commit b601d68

Browse files
committed
Return rest error in decode function
1 parent 775e4e1 commit b601d68

1 file changed

Lines changed: 15 additions & 22 deletions

File tree

rest/decode.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ import (
1717
// - any unknown fields were set
1818
// - deserialization fails
1919
// - the OK() method returns an error
20-
func Decode(w http.ResponseWriter, r *http.Request, dst interface{}) error {
20+
func Decode(w http.ResponseWriter, r *http.Request, dst interface{}) *Error {
2121
if r.Header.Get("Content-Type") != "" {
2222
value, _ := header.ParseValueAndParams(r.Header, "Content-Type")
2323
if value != "application/json" {
2424
msg := "Content-Type header is not application/json"
25-
return &MalformedRequest{Status: http.StatusUnsupportedMediaType, Message: msg}
25+
err := fmt.Errorf("wrong or missing Content-Type header value")
26+
return &Error{Err: err, Status: http.StatusUnsupportedMediaType, Message: msg}
2627
}
2728
}
2829

@@ -38,58 +39,50 @@ func Decode(w http.ResponseWriter, r *http.Request, dst interface{}) error {
3839

3940
switch {
4041
case errors.As(err, &syntaxError):
41-
msg := fmt.Sprintf("Request body contains badly-formed JSON (at position %d)", syntaxError.Offset)
42-
return &MalformedRequest{Status: http.StatusBadRequest, Message: msg}
42+
msg := fmt.Sprintf("request body contains badly-formed JSON (at position %d)", syntaxError.Offset)
43+
return &Error{Err: err, Status: http.StatusBadRequest, Message: msg}
4344

4445
case errors.Is(err, io.ErrUnexpectedEOF):
45-
msg := fmt.Sprintf("Request body contains badly-formed JSON")
46-
return &MalformedRequest{Status: http.StatusBadRequest, Message: msg}
46+
msg := fmt.Sprintf("request body contains badly-formed JSON")
47+
return &Error{Err: err, Status: http.StatusBadRequest, Message: msg}
4748

4849
case errors.As(err, &unmarshalTypeError):
4950
msg := fmt.Sprintf("Request body contains an invalid value for the %q field (at position %d)", unmarshalTypeError.Field, unmarshalTypeError.Offset)
50-
return &MalformedRequest{Status: http.StatusBadRequest, Message: msg}
51+
return &Error{Err: err, Status: http.StatusBadRequest, Message: msg}
5152

5253
case strings.HasPrefix(err.Error(), "json: unknown field "):
5354
fieldName := strings.TrimPrefix(err.Error(), "json: unknown field ")
5455
msg := fmt.Sprintf("Request body contains unknown field %s", fieldName)
55-
return &MalformedRequest{Status: http.StatusBadRequest, Message: msg}
56+
return &Error{Err: err, Status: http.StatusBadRequest, Message: msg}
5657

5758
case errors.Is(err, io.EOF):
5859
msg := "Request body must not be empty"
59-
return &MalformedRequest{Status: http.StatusBadRequest, Message: msg}
60+
return &Error{Err: err, Status: http.StatusBadRequest, Message: msg}
6061

6162
case err.Error() == "http: request body too large":
6263
msg := "Request body must not be larger than 1MB"
63-
return &MalformedRequest{Status: http.StatusRequestEntityTooLarge, Message: msg}
64+
return &Error{Err: err, Status: http.StatusRequestEntityTooLarge, Message: msg}
6465

6566
default:
66-
return err
67+
msg := fmt.Sprintf("Unknown error while decoding the request: %v", err.Error())
68+
return &Error{Err: err, Status: http.StatusBadRequest, Message: msg}
6769
}
6870
}
6971

7072
err = dec.Decode(&struct{}{})
7173
if err != io.EOF {
7274
msg := "Request body must only contain a single JSON object"
73-
return &MalformedRequest{Status: http.StatusBadRequest, Message: msg}
75+
return &Error{Err: err, Status: http.StatusBadRequest, Message: msg}
7476
}
7577

7678
if valid, ok := dst.(interface {
7779
OK() error
7880
}); ok {
7981
err = valid.OK()
8082
if err != nil {
81-
return &MalformedRequest{Status: http.StatusBadRequest, Message: fmt.Sprintf("validating the decoded object failed: %v", err.Error())}
83+
return &Error{Err: err, Status: http.StatusBadRequest, Message: fmt.Sprintf("validating the decoded object failed: %v", err.Error())}
8284
}
8385
}
8486

8587
return nil
8688
}
87-
88-
type MalformedRequest struct {
89-
Status int
90-
Message string
91-
}
92-
93-
func (mr *MalformedRequest) Error() string {
94-
return mr.Message
95-
}

0 commit comments

Comments
 (0)