2023-08-09 14:31:23 +03:00
|
|
|
package encoders
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2023-08-11 21:27:38 +03:00
|
|
|
pb "go.unistack.org/unistack-org/pkgdash/proto"
|
|
|
|
|
2023-08-09 14:31:23 +03:00
|
|
|
"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}))
|
|
|
|
}
|