Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion utils/xtwirp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
30 changes: 30 additions & 0 deletions utils/xtwirp/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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())
})
}
}
Loading