Skip to content

Commit

Permalink
Added gRPC error message as log message
Browse files Browse the repository at this point in the history
  • Loading branch information
sp-joseluis-cerrada committed Aug 25, 2023
1 parent ca5680c commit bea2bc1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions grpcx/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ func WithErrorLogsStream(l logx.Logger, options ...ErrorLogsOption) grpc.StreamS
{Key: "ctx_error_code", Value: status.Code(err)},
}
if inCodeList(errCode, logOptions.debugLevelCodes) {
l.Info("gRPC Error", fields...)
l.Info("gRPC Error: "+err.Error(), fields...)
} else {
l.Error("gRPC Error", fields...)
l.Error("gRPC Error: "+err.Error(), fields...)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions grpcx/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func TestWithErrorLogsStream(t *testing.T) {

// assert
a.Error(err)
a.Contains(w.String(), fmt.Sprintf(`ERRO gRPC Error FIELDS ctx_full_method=%s ctx_response_error_code=%s ctx_response_error_message=%s`, method, expectedCode.String(), expectedErr.Error()))
a.Contains(w.String(), fmt.Sprintf(`ERRO gRPC Error: rpc error: code = Unknown desc = some error FIELDS ctx_full_method=%s ctx_response_error_code=%s ctx_response_error_message=%s`, method, expectedCode.String(), expectedErr.Error()))
})

t.Run("logs error on debug level if custom options added", func(t *testing.T) {
Expand All @@ -314,7 +314,7 @@ func TestWithErrorLogsStream(t *testing.T) {

// assert
a.Error(err)
a.Contains(w.String(), fmt.Sprintf(`INFO gRPC Error FIELDS ctx_full_method=%s ctx_response_error_code=%s ctx_response_error_message=%s`, method, expectedCode.String(), expectedErr.Error()))
a.Contains(w.String(), fmt.Sprintf(`INFO gRPC Error: rpc error: code = NotFound desc = some error FIELDS ctx_full_method=%s ctx_response_error_code=%s ctx_response_error_message=%s`, method, expectedCode.String(), expectedErr.Error()))
})

t.Run("do not log error if discarded options added", func(t *testing.T) {
Expand Down

0 comments on commit bea2bc1

Please sign in to comment.