Improve read.ProtoJSON bad protobuf body error handling

This commit is contained in:
Herman Slatman 2022-04-19 12:07:57 +02:00
parent 647538e9e8
commit 6532c93303
No known key found for this signature in database
GPG key ID: F4D8A44EA0A75A4F

View file

@ -10,6 +10,7 @@ import (
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"github.com/smallstep/certificates/api/render"
"github.com/smallstep/certificates/errs" "github.com/smallstep/certificates/errs"
) )
@ -29,49 +30,35 @@ func ProtoJSON(r io.Reader, m proto.Message) error {
if err != nil { if err != nil {
return errs.BadRequestErr(err, "error reading request body") return errs.BadRequestErr(err, "error reading request body")
} }
if err := protojson.Unmarshal(data, m); err != nil {
if errors.Is(err, proto.Error) { switch err := protojson.Unmarshal(data, m); {
return newBadProtoJSONError(err) case errors.Is(err, proto.Error):
} return badProtoJSONError(err.Error())
default:
return err
} }
return err
} }
// BadProtoJSONError is an error type that is used when a proto // badProtoJSONError is an error type that is returned by ProtoJSON
// message cannot be unmarshaled. Usually this is caused by an error // when a proto message cannot be unmarshaled. Usually this is caused
// in the request body. // by an error in the request body.
type BadProtoJSONError struct { type badProtoJSONError string
err error
Type string `json:"type"` // Error implements error for badProtoJSONError
Detail string `json:"detail"` func (e badProtoJSONError) Error() string {
Message string `json:"message"` return string(e)
} }
// newBadProtoJSONError returns a new instance of BadProtoJSONError // Render implements render.RenderableError for badProtoJSONError
// This error type is always caused by an error in the request body. func (e badProtoJSONError) Render(w http.ResponseWriter) {
func newBadProtoJSONError(err error) *BadProtoJSONError { v := struct {
return &BadProtoJSONError{ Type string `json:"type"`
err: err, Detail string `json:"detail"`
Message string `json:"message"`
}{
Type: "badRequest", Type: "badRequest",
Detail: "bad request", Detail: "bad request",
Message: err.Error(), Message: e.Error(),
} }
} render.JSONStatus(w, v, http.StatusBadRequest)
// Error implements the error interface
func (e *BadProtoJSONError) Error() string {
return e.err.Error()
}
// Render implements render.RenderableError for BadProtoError
func (e *BadProtoJSONError) Render(w http.ResponseWriter) {
errData, err := json.Marshal(e)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write(errData)
} }