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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user