From 5908678ff129010909473bb3417e2b6aa353ea58 Mon Sep 17 00:00:00 2001 From: Genseric Ghiro Date: Tue, 22 Sep 2026 14:47:08 -0400 Subject: [PATCH] Removing twirp error unknown prefix --- utils/xtwirp/errors.go | 2 +- utils/xtwirp/errors_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/utils/xtwirp/errors.go b/utils/xtwirp/errors.go index 14da5e144..a28736d40 100644 --- a/utils/xtwirp/errors.go +++ b/utils/xtwirp/errors.go @@ -71,7 +71,7 @@ func WithDetailsFrom(dst twirp.Error, src error) twirp.Error { return dst } if dst.Code() == twirp.Unknown { - dst = twirp.NewError(ErrorCodeFromGRPC(st.Code()), dst.Error()) + dst = twirp.NewError(ErrorCodeFromGRPC(st.Code()), dst.Msg()) } return WithDetailsFromStatus(dst, st) } diff --git a/utils/xtwirp/errors_test.go b/utils/xtwirp/errors_test.go index ddf247a3b..63095539e 100644 --- a/utils/xtwirp/errors_test.go +++ b/utils/xtwirp/errors_test.go @@ -10,6 +10,7 @@ import ( "google.golang.org/grpc/status" "github.com/livekit/protocol/utils/xtwirp" + "github.com/livekit/psrpc" ) func TestStatus(t *testing.T) { @@ -25,3 +26,32 @@ func TestStatus(t *testing.T) { require.Equal(t, st, got) require.Equal(t, st.Details(), got.Details()) } + +// A non-twirp error is first wrapped as twirp.Unknown, then re-coded once the +// gRPC status is known. The re-coding must not fold the "twirp error unknown:" +// prefix into the message. +func TestToErrorKeepsMessage(t *testing.T) { + cases := []struct { + name string + err error + msg string + }{ + { + name: "psrpc", + err: psrpc.NewErrorFromResponse(string(psrpc.NotFound), "object cannot be found"), + msg: "object cannot be found", + }, + { + name: "grpc status", + err: status.Error(codes.NotFound, "object cannot be found"), + msg: "rpc error: code = NotFound desc = object cannot be found", + }, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + e := xtwirp.ToError(c.err) + require.Equal(t, twirp.NotFound, e.Code()) + require.Equal(t, c.msg, e.Msg()) + }) + } +}