From 027980a8c1310b2d07d2b4138c681ae55b6952e6 Mon Sep 17 00:00:00 2001 From: Martin Schneppenheim Date: Sat, 19 Dec 2020 18:39:20 +0100 Subject: [PATCH] Export malformed request properties --- rest/decode.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/rest/decode.go b/rest/decode.go index 9a2d0bf..1474893 100644 --- a/rest/decode.go +++ b/rest/decode.go @@ -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} } } @@ -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 @@ -70,7 +70,7 @@ 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 { @@ -78,7 +78,7 @@ func Decode(w http.ResponseWriter, r *http.Request, dst interface{}) 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())} } } @@ -86,10 +86,10 @@ func Decode(w http.ResponseWriter, r *http.Request, dst interface{}) error { } type MalformedRequest struct { - status int - msg string + Status int + Message string } func (mr *MalformedRequest) Error() string { - return mr.msg + return mr.Message }