From ba2922c200a17081ac58c6e9f9195426dce29cff Mon Sep 17 00:00:00 2001
From: Paul Wells <paulwe@gmail.com>
Date: Wed, 20 Nov 2024 13:30:51 -0800
Subject: [PATCH] add error codes to create participant input validation errors
 (#230)

---
 pkg/config/config.go |  4 ++--
 pkg/sip/client.go    | 19 +++++++++----------
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/pkg/config/config.go b/pkg/config/config.go
index 8830752..6dfa5aa 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -27,7 +27,7 @@ import (
 	"github.com/livekit/mediatransportutil/pkg/rtcconfig"
 	"github.com/livekit/protocol/logger"
 	"github.com/livekit/protocol/redis"
-	"github.com/livekit/protocol/utils"
+	"github.com/livekit/protocol/utils/guid"
 	"github.com/livekit/psrpc"
 	lksdk "github.com/livekit/server-sdk-go/v2"
 
@@ -115,7 +115,7 @@ func NewConfig(confString string) (*Config, error) {
 }
 
 func (c *Config) Init() error {
-	c.NodeID = utils.NewGuid("NE_")
+	c.NodeID = guid.New("NE_")
 
 	if c.SIPPort == 0 {
 		c.SIPPort = DefaultSIPPort
diff --git a/pkg/sip/client.go b/pkg/sip/client.go
index 0b05324..3be9ef6 100644
--- a/pkg/sip/client.go
+++ b/pkg/sip/client.go
@@ -16,8 +16,6 @@ package sip
 
 import (
 	"context"
-	"errors"
-	"fmt"
 	"log/slog"
 	"net/netip"
 	"strings"
@@ -31,6 +29,7 @@ import (
 	"github.com/livekit/protocol/logger"
 	"github.com/livekit/protocol/rpc"
 	"github.com/livekit/protocol/tracer"
+	"github.com/livekit/psrpc"
 	"github.com/livekit/sipgo"
 	"github.com/livekit/sipgo/sip"
 
@@ -132,25 +131,25 @@ func (c *Client) createSIPParticipant(ctx context.Context, req *rpc.InternalCrea
 		return nil, siperrors.ErrUnavailable
 	}
 	if req.CallTo == "" {
-		return nil, fmt.Errorf("call-to number must be set")
+		return nil, psrpc.NewErrorf(psrpc.InvalidArgument, "call-to number must be set")
 	} else if req.Address == "" {
-		return nil, fmt.Errorf("trunk adresss must be set")
+		return nil, psrpc.NewErrorf(psrpc.InvalidArgument, "trunk adresss must be set")
 	} else if req.Number == "" {
-		return nil, fmt.Errorf("trunk outbound number must be set")
+		return nil, psrpc.NewErrorf(psrpc.InvalidArgument, "trunk outbound number must be set")
 	} else if req.RoomName == "" {
-		return nil, fmt.Errorf("room name must be set")
+		return nil, psrpc.NewErrorf(psrpc.InvalidArgument, "room name must be set")
 	}
 	if strings.Contains(req.CallTo, "@") {
-		return nil, errors.New("call_to should be a phone number or SIP user, not a full SIP URI")
+		return nil, psrpc.NewErrorf(psrpc.InvalidArgument, "call_to should be a phone number or SIP user, not a full SIP URI")
 	}
 	if strings.HasPrefix(req.Address, "sip:") || strings.HasPrefix(req.Address, "sips:") {
-		return nil, errors.New("address must be a hostname without 'sip:' prefix")
+		return nil, psrpc.NewErrorf(psrpc.InvalidArgument, "address must be a hostname without 'sip:' prefix")
 	}
 	if strings.Contains(req.Address, "transport=") {
-		return nil, errors.New("address must not contain parameters; use transport field")
+		return nil, psrpc.NewErrorf(psrpc.InvalidArgument, "address must not contain parameters; use transport field")
 	}
 	if strings.ContainsAny(req.Address, ";=") {
-		return nil, errors.New("address must not contain parameters")
+		return nil, psrpc.NewErrorf(psrpc.InvalidArgument, "address must not contain parameters")
 	}
 	log := c.log
 	if req.ProjectId != "" {