pkgdash/internal/handler/handler.go

57 lines
1.2 KiB
Go
Raw Normal View History

package handler
import (
"net/http"
"strconv"
"github.com/google/uuid"
jsonpbcodec "go.unistack.org/micro-codec-jsonpb/v3"
"go.unistack.org/micro/v3/codec"
"go.unistack.org/micro/v3/logger"
"go.unistack.org/pkgdash/internal/storage"
pb "go.unistack.org/pkgdash/proto"
)
type Handler struct {
logger logger.Logger
store storage.Storage
codec codec.Codec
}
func NewNotFoundError(err error) *pb.ErrorRsp {
return &pb.ErrorRsp{
Code: strconv.Itoa(http.StatusBadRequest),
Title: "NotFound",
Uuid: uuid.New().String(),
Details: err.Error(),
}
}
func NewInternalError(err error) *pb.ErrorRsp {
return &pb.ErrorRsp{
Code: strconv.Itoa(http.StatusInternalServerError),
Title: "InternalServerError",
Uuid: uuid.New().String(),
Details: err.Error(),
}
}
func NewValidationError(err error) *pb.ErrorRsp {
return &pb.ErrorRsp{
Code: strconv.Itoa(http.StatusBadRequest),
Title: "BadRequest",
Uuid: uuid.New().String(),
Details: err.Error(),
}
}
func NewHandler(log logger.Logger, store storage.Storage) (*Handler, error) {
h := &Handler{
logger: log,
codec: jsonpbcodec.NewCodec(),
store: store,
}
return h, nil
}