@@ -23,7 +23,7 @@ const (
2323
2424type statusError struct {
2525 error
26- * status.Status
26+ s * status.Status
2727 level Level
2828 logger Logger
2929}
@@ -44,14 +44,14 @@ func WithLogger(handler Logger) ErrorOption { //nolint:ireturn
4444}
4545
4646func (o * newOptionDetails ) apply (e * statusError ) {
47- statusWithDetails , err := e .WithDetails (o .details ... )
47+ statusWithDetails , err := e .s . WithDetails (o .details ... )
4848 if err != nil {
49- err = errorz .NewErrorf (errorz .WithCallerSkip (2 ))("e.WithDetails: details=%v: %w" , o .details , err )
49+ err = errorz .NewErrorf (errorz .WithCallerSkip (2 ))("e.s. WithDetails: details=%v: %w" , o .details , err )
5050 e .logger (context .Background (), ErrorLevel , status .New (codes .Unknown , "WithDetails failed" ), err )
5151 return
5252 }
5353
54- e .Status = statusWithDetails
54+ e .s = statusWithDetails
5555}
5656
5757func WithDetails (details ... protoiface.MessageV1 ) ErrorOption { //nolint:ireturn
@@ -64,19 +64,33 @@ var (
6464 DefaultLogger Logger = func (_ context.Context , level Level , stat * status.Status , err error ) {
6565 log .Printf ("level=%s code=%s message=%q details=%s error=%q stacktrace=%q" , level , stat .Code (), stat .Message (), stat .Details (), fmt .Sprintf ("%v" , err ), fmt .Sprintf ("%+v" , err ))
6666 }
67- errorf = errorz .NewErrorf (errorz .WithCallerSkip (1 ))
67+ _ interface { GRPCStatus () * status.Status } = (* statusError )(nil )
68+ errorf = errorz .NewErrorf (errorz .WithCallerSkip (1 ))
6869)
6970
7071func New (ctx context.Context , level Level , code codes.Code , msg string , err error , opts ... ErrorOption ) error {
7172 e := & statusError {
7273 error : errorf ("errgrpc.New: level=%s code=%s message=%s: %w" , level , code , msg , err ),
73- Status : status .New (code , msg ),
74+ s : status .New (code , msg ),
7475 level : level ,
7576 logger : DefaultLogger ,
7677 }
7778 for _ , opt := range opts {
7879 opt .apply (e )
7980 }
80- e .logger (ctx , e .level , e .Status , err )
81- return errorf ("%s: %s" , msg , err )
81+ e .logger (ctx , e .level , e .s , err )
82+ return e
83+ }
84+
85+ func (e * statusError ) Format (s fmt.State , verb rune ) {
86+ if formatter , ok := e .error .(fmt.Formatter ); ok { //nolint:errorlint
87+ formatter .Format (s , verb )
88+ return
89+ }
90+
91+ _ , _ = fmt .Fprintf (s , fmt .FormatString (s , verb ), e .error )
92+ }
93+
94+ func (e * statusError ) GRPCStatus () * status.Status {
95+ return e .s
8296}
0 commit comments