You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/middleware/status/status.go

160 lines
3.5 KiB

4 years ago
package status
import (
"context"
"net/http"
4 years ago
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/middleware"
//lint:ignore SA1019 grpc
4 years ago
"github.com/golang/protobuf/proto"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type domainKey struct{}
4 years ago
// HandlerFunc is middleware error handler.
type HandlerFunc func(context.Context, error) error
4 years ago
// Option is recovery option.
type Option func(*options)
type options struct {
domain string
4 years ago
handler HandlerFunc
}
// WithDomain with service domain.
func WithDomain(domain string) Option {
return func(o *options) {
o.domain = domain
}
}
4 years ago
// WithHandler with status handler.
func WithHandler(h HandlerFunc) Option {
return func(o *options) {
o.handler = h
}
}
// Server is an error middleware.
func Server(opts ...Option) middleware.Middleware {
options := options{
handler: encodeErr,
4 years ago
}
for _, o := range opts {
o(&options)
}
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
reply, err := handler(ctx, req)
if err != nil {
ctx = context.WithValue(ctx, domainKey{}, options.domain)
return nil, options.handler(ctx, err)
4 years ago
}
return reply, nil
}
}
}
// Client is an error middleware.
func Client(opts ...Option) middleware.Middleware {
options := options{
handler: decodeErr,
4 years ago
}
for _, o := range opts {
o(&options)
}
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
reply, err := handler(ctx, req)
if err != nil {
return nil, options.handler(ctx, err)
4 years ago
}
return reply, nil
}
}
}
func encodeErr(ctx context.Context, err error) error {
se := errors.FromError(err)
if se.Domain == "" {
se.Domain, _ = ctx.Value(domainKey{}).(string)
4 years ago
}
gs := status.Newf(httpToGRPCCode(se.Code), "%s: %s", se.Reason, se.Message)
4 years ago
details := []proto.Message{
&errdetails.ErrorInfo{
Domain: se.Domain,
4 years ago
Reason: se.Reason,
Metadata: se.Metadata,
4 years ago
},
}
gs, err = gs.WithDetails(details...)
if err != nil {
return err
}
return gs.Err()
}
func decodeErr(ctx context.Context, err error) error {
4 years ago
gs := status.Convert(err)
se := &errors.Error{
Code: grpcToHTTPCode(gs.Code()),
Message: gs.Message(),
4 years ago
}
for _, detail := range gs.Details() {
switch d := detail.(type) {
case *errdetails.ErrorInfo:
se.Domain = d.Domain
4 years ago
se.Reason = d.Reason
se.Metadata = d.Metadata
4 years ago
return se
}
}
return se
}
func httpToGRPCCode(code int) codes.Code {
switch code {
case http.StatusBadRequest:
return codes.InvalidArgument
case http.StatusUnauthorized:
return codes.Unauthenticated
case http.StatusForbidden:
return codes.PermissionDenied
case http.StatusNotFound:
return codes.NotFound
case http.StatusConflict:
return codes.Aborted
case http.StatusInternalServerError:
return codes.Internal
case http.StatusServiceUnavailable:
return codes.Unavailable
}
return codes.Unknown
}
func grpcToHTTPCode(code codes.Code) int {
switch code {
case codes.InvalidArgument:
return http.StatusBadRequest
case codes.Unauthenticated:
return http.StatusUnauthorized
case codes.PermissionDenied:
return http.StatusForbidden
case codes.NotFound:
return http.StatusNotFound
case codes.Aborted:
return http.StatusConflict
case codes.Internal:
return http.StatusInternalServerError
case codes.Unavailable:
return http.StatusServiceUnavailable
}
return http.StatusInternalServerError
}