Skip to content

Commit d032eec

Browse files
authored
Implementing psrpc mapping to proper codes (#538)
1 parent 7e6521e commit d032eec

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pkg/errors/errors.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
package errors
1616

1717
import (
18+
"errors"
19+
20+
"github.com/livekit/protocol/livekit"
1821
"github.com/livekit/psrpc"
1922
)
2023

@@ -26,3 +29,14 @@ var (
2629
func ErrCouldNotParseConfig(err error) psrpc.Error {
2730
return psrpc.NewErrorf(psrpc.InvalidArgument, "could not parse config: %v", err)
2831
}
32+
33+
// If the provided error wraps a livekit.SIPStatus, this will apply the error code mapping to the resulting error
34+
func ApplySIPStatus(err error) error {
35+
var sipStatus *livekit.SIPStatus
36+
if !errors.As(err, &sipStatus) {
37+
return err
38+
}
39+
code := sipStatus.GRPCStatus().Code()
40+
err = psrpc.NewError(psrpc.ErrorCodeFromGRPC(code), err, sipStatus)
41+
return err
42+
}

pkg/sip/service.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/livekit/sipgo"
4040

4141
"github.com/livekit/sip/pkg/config"
42+
siperrors "github.com/livekit/sip/pkg/errors"
4243
"github.com/livekit/sip/pkg/stats"
4344
"github.com/livekit/sip/version"
4445
)
@@ -267,7 +268,8 @@ func (s *Service) Start() error {
267268
}
268269

269270
func (s *Service) CreateSIPParticipant(ctx context.Context, req *rpc.InternalCreateSIPParticipantRequest) (*rpc.InternalCreateSIPParticipantResponse, error) {
270-
return s.cli.CreateSIPParticipant(ctx, req)
271+
resp, err := s.cli.CreateSIPParticipant(ctx, req)
272+
return resp, siperrors.ApplySIPStatus(err)
271273
}
272274

273275
func (s *Service) CreateSIPParticipantAffinity(ctx context.Context, req *rpc.InternalCreateSIPParticipantRequest) float32 {
@@ -276,6 +278,11 @@ func (s *Service) CreateSIPParticipantAffinity(ctx context.Context, req *rpc.Int
276278
}
277279

278280
func (s *Service) TransferSIPParticipant(ctx context.Context, req *rpc.InternalTransferSIPParticipantRequest) (*emptypb.Empty, error) {
281+
resp, err := s.transferSIPParticipant(ctx, req)
282+
return resp, siperrors.ApplySIPStatus(err)
283+
}
284+
285+
func (s *Service) transferSIPParticipant(ctx context.Context, req *rpc.InternalTransferSIPParticipantRequest) (*emptypb.Empty, error) {
279286
s.log.Infow("transferring SIP call", "callID", req.SipCallId, "transferTo", req.TransferTo)
280287

281288
// Check if provider is internal and config is set before allowing transfer

0 commit comments

Comments
 (0)