Skip to content

Commit adaba38

Browse files
committed
fix(chip-router): always give a rejected Publish a non-empty error message
A subscriber may reject an event without a PublishError.Reason, which made the router return a Publish error with an empty gRPC message. Prefix the message ('event rejected by subscribers: <reason>') so it is never empty.
1 parent ffd92be commit adaba38

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎framework/components/chiprouter/cmd/chip-router/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (r *router) Publish(ctx context.Context, event *cepb.CloudEvent) (*chippb.P
210210

211211
if !accepted {
212212
if rejection != nil {
213-
return nil, status.Error(codes.Internal, rejection.GetReason())
213+
return nil, status.Errorf(codes.Internal, "event rejected by subscribers: %s", rejection.GetReason())
214214
}
215215
return nil, status.Error(codes.Internal, fmt.Sprintf("all %d subscribers failed to accept the event", len(snapshot)))
216216
}

‎framework/components/chiprouter/cmd/chip-router/main_test.go‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,24 @@ func TestPublishAllSubscribersRejectedFailsWithInternal(t *testing.T) {
389389

390390
_, err := r.Publish(t.Context(), eventOf("e1"))
391391
requireCode(t, err, codes.Internal)
392-
if got := status.Convert(err).Message(); got != "invalid event" {
393-
t.Errorf("error message = %q, want the subscriber's rejection reason %q", got, "invalid event")
392+
if got, want := status.Convert(err).Message(), "event rejected by subscribers: invalid event"; got != want {
393+
t.Errorf("error message = %q, want %q", got, want)
394+
}
395+
}
396+
397+
func TestPublishRejectionWithoutReasonStillHasMessage(t *testing.T) {
398+
// A subscriber may reject without a Reason; the caller must not see an
399+
// empty gRPC error message.
400+
r := &router{subscribers: map[string]*subscriber{
401+
"sink": {id: "sink", client: &stubPublishClient{response: &chippb.PublishResponse{
402+
Results: []*chippb.PublishResult{rejected("e1", "")},
403+
}}},
404+
}}
405+
406+
_, err := r.Publish(t.Context(), eventOf("e1"))
407+
requireCode(t, err, codes.Internal)
408+
if got := status.Convert(err).Message(); got == "" {
409+
t.Error("error message is empty, want a non-empty message")
394410
}
395411
}
396412

0 commit comments

Comments
 (0)