pkgdash/handler/encoders/json.go
Vasiliy Tolstov 2ef12956ac initial rewrite, use micro v4 and not cms-xxx stuff
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-11 21:27:38 +03:00

27 lines
656 B
Go

package encoders
import (
"encoding/json"
"net/http"
pb "go.unistack.org/unistack-org/pkgdash/proto"
"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}))
}