pkgdash/handler/encoders/json.go

26 lines
667 B
Go

package encoders
import (
"encoding/json"
pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate"
"net/http"
"github.com/pkg/errors"
)
type JSON struct{}
func (*JSON) Success(rw http.ResponseWriter, response interface{}) error {
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.WriteHeader(http.StatusOK)
return errors.WithStack(json.NewEncoder(rw).Encode(response))
}
func (*JSON) Error(rw http.ResponseWriter, err *pb.Error, status int) error {
rw.Header().Set("Content-Type", "application/problem+json; charset=utf-8")
rw.WriteHeader(status)
return errors.WithStack(json.NewEncoder(rw).Encode(&pb.ErrorRsp{Error: err}))
}