Skip to content

Commit

Permalink
Merge pull request #91 from alecsammon/unknown_body_type_error
Browse files Browse the repository at this point in the history
Add custom ErrUnknownBodyFormat
  • Loading branch information
ggicci authored Jan 14, 2024
2 parents 5d958eb + 1b09f0b commit fbcb1f2
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions core/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ import (
"github.com/ggicci/httpin/internal"
)

// ErrUnknownBodyFormat is returned when a serializer for the specified body format has not been specified.
var ErrUnknownBodyFormat = errors.New("unknown body format")

// DirectiveBody is the implementation of the "body" directive.
type DirectiveBody struct{}

func (db *DirectiveBody) Decode(rtm *DirectiveRuntime) error {
req := rtm.GetRequest()
bodyFormat, bodySerializer := db.getSerializer(rtm)
if bodySerializer == nil {
return fmt.Errorf("unknown body format: %q", bodyFormat)
return fmt.Errorf("%w: %q", ErrUnknownBodyFormat, bodyFormat)
}
if err := bodySerializer.Decode(req.Body, rtm.Value.Elem().Addr().Interface()); err != nil {
return err
Expand All @@ -33,7 +36,7 @@ func (db *DirectiveBody) Decode(rtm *DirectiveRuntime) error {
func (db *DirectiveBody) Encode(rtm *DirectiveRuntime) error {
bodyFormat, bodySerializer := db.getSerializer(rtm)
if bodySerializer == nil {
return fmt.Errorf("unknown body format: %q", bodyFormat)
return fmt.Errorf("%w: %q", ErrUnknownBodyFormat, bodyFormat)
}
if bodyReader, err := bodySerializer.Encode(rtm.Value.Interface()); err != nil {
return err
Expand Down

0 comments on commit fbcb1f2

Please sign in to comment.