1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-11-06 08:59:18 +02:00

feat: Support custom status code conversion from HTTP and gRPC. (#1410)

* feat: Support custom status code conversion from HTTP and gRPC.

Co-authored-by: Letian Yi <yiletian@webull.com>
This commit is contained in:
letian
2021-09-05 23:25:33 +08:00
committed by GitHub
parent 1ac50be94c
commit fa54a1dd3a
5 changed files with 185 additions and 151 deletions

View File

@@ -4,7 +4,8 @@ import (
"errors"
"fmt"
"github.com/go-kratos/kratos/v2/internal/httputil"
httpstatus "github.com/go-kratos/kratos/v2/transport/http/status"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
@@ -27,7 +28,7 @@ func (e *Error) Error() string {
// GRPCStatus returns the Status represented by se.
func (e *Error) GRPCStatus() *status.Status {
s, _ := status.New(httputil.GRPCCodeFromStatus(int(e.Code)), e.Message).
s, _ := status.New(httpstatus.ToGRPCCode(int(e.Code)), e.Message).
WithDetails(&errdetails.ErrorInfo{
Reason: e.Reason,
Metadata: e.Metadata,
@@ -105,7 +106,7 @@ func FromError(err error) *Error {
switch d := detail.(type) {
case *errdetails.ErrorInfo:
return New(
httputil.StatusFromGRPCCode(gs.Code()),
httpstatus.FromGRPCCode(gs.Code()),
d.Reason,
gs.Message(),
).WithMetadata(d.Metadata)