Skip to content

Commit

Permalink
Add UnencodableEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed Jun 19, 2024
1 parent a938bc3 commit d313713
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package hrt
import (
"encoding/json"
"net/http"

"github.com/pkg/errors"
)

// DefaultEncoder is the default encoder used by the router. It decodes GET
Expand Down Expand Up @@ -33,6 +35,8 @@ type CombinedEncoder struct {
Decoder Decoder
}

var _ Encoder = CombinedEncoder{}

// Encode implements the Encoder interface.
func (e CombinedEncoder) Encode(w http.ResponseWriter, v any) error {
return e.Encoder.Encode(w, v)
Expand All @@ -43,6 +47,20 @@ func (e CombinedEncoder) Decode(r *http.Request, v any) error {
return e.Decoder.Decode(r, v)
}

// UnencodableEncoder is an encoder that can only decode and not encode.
// It wraps an existing decoder.
// Calling Encode will return a 500 error, as it is considered a bug to return
// anything.
type UnencodableEncoder struct {
Decoder
}

var _ Encoder = UnencodableEncoder{}

func (e UnencodableEncoder) Encode(w http.ResponseWriter, v any) error {
return WrapHTTPError(http.StatusInternalServerError, errors.New("cannot encode"))
}

// JSONEncoder is an encoder that encodes and decodes JSON.
var JSONEncoder Encoder = jsonEncoder{}

Expand Down

0 comments on commit d313713

Please sign in to comment.