forked from TrueCloudLab/certificates
Improve read.ProtoJSON bad protobuf body error handling
This commit is contained in:
parent
647538e9e8
commit
6532c93303
1 changed files with 24 additions and 37 deletions
|
@ -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
|
|
||||||
|
// Error implements error for badProtoJSONError
|
||||||
|
func (e badProtoJSONError) Error() string {
|
||||||
|
return string(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render implements render.RenderableError for badProtoJSONError
|
||||||
|
func (e badProtoJSONError) Render(w http.ResponseWriter) {
|
||||||
|
v := struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Detail string `json:"detail"`
|
Detail string `json:"detail"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}{
|
||||||
|
|
||||||
// newBadProtoJSONError returns a new instance of BadProtoJSONError
|
|
||||||
// This error type is always caused by an error in the request body.
|
|
||||||
func newBadProtoJSONError(err error) *BadProtoJSONError {
|
|
||||||
return &BadProtoJSONError{
|
|
||||||
err: err,
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue