From 477d263328addff09a7ccb6b1e83c86fa8778d06 Mon Sep 17 00:00:00 2001 From: pnguyen215 Date: Sat, 23 Sep 2023 09:25:00 +0700 Subject: [PATCH] :recycle: refactor: updated codebase #14 --- pkg/ami/ami.go | 18 +++++++-------- pkg/ami/ami_error.go | 43 ++++++++++++++++++++++++++++++++++++ pkg/ami/ami_helper.go | 7 +++--- pkg/ami/config/ami_config.go | 23 ------------------- pkg/ami/fatal/ami_fatal.go | 24 -------------------- 5 files changed, 55 insertions(+), 60 deletions(-) create mode 100644 pkg/ami/ami_error.go delete mode 100644 pkg/ami/fatal/ami_fatal.go diff --git a/pkg/ami/ami.go b/pkg/ami/ami.go index 722dc42..28e1ea1 100644 --- a/pkg/ami/ami.go +++ b/pkg/ami/ami.go @@ -77,12 +77,12 @@ func (c *AMI) ReadPrompt(parentCtx context.Context, timeout time.Duration) error select { case <-ctx.Done(): - return config.ErrorAsteriskConnTimeout + return ErrorAsteriskConnTimeout case err := <-fail: - return config.ErrorAsteriskNetwork.AMIError(err.Error()) + return ErrorAsteriskNetwork.AMIError(err.Error()) case promptLine := <-prompt: if !strings.HasPrefix(promptLine, config.AmiCallManagerKey) { - return config.ErrorAsteriskInvalidPrompt.AMIError(promptLine) + return ErrorAsteriskInvalidPrompt.AMIError(promptLine) } } @@ -93,8 +93,8 @@ func (c *AMI) Read() (*AMIMessage, error) { headers, err := c.Reader.ReadMIMEHeader() if err != nil { - if err.Error() == config.ErrorEOF || err.Error() == config.ErrorIO { - return nil, config.ErrorAsteriskNetwork + if err.Error() == ErrorEOF || err.Error() == ErrorIO { + return nil, ErrorAsteriskNetwork } return nil, err } @@ -156,12 +156,12 @@ func (c *AMI) Login(parentContext context.Context, timeout time.Duration, userna select { case <-ctx.Done(): - return config.ErrorAsteriskConnTimeout.AMIError(config.ErrorLoginFailed) + return ErrorAsteriskConnTimeout.AMIError(ErrorLoginFailed) case err := <-fail: - return config.ErrorAsteriskNetwork.AMIError(err.Error()) + return ErrorAsteriskNetwork.AMIError(err.Error()) case msg := <-chMessage: if !msg.IsSuccess() { - return config.ErrorAsteriskLogin + return ErrorAsteriskLogin } else { // add callback value c.Raw = msg @@ -211,7 +211,7 @@ func (c *AMI) SetReader(ctx context.Context) { } if err != nil { - if err == config.ErrorAsteriskNetwork { + if err == ErrorAsteriskNetwork { c.EmitError(err) return } diff --git a/pkg/ami/ami_error.go b/pkg/ami/ami_error.go new file mode 100644 index 0000000..da6aac4 --- /dev/null +++ b/pkg/ami/ami_error.go @@ -0,0 +1,43 @@ +package ami + +import "fmt" + +var ( + // ErrorAsteriskConnTimeout error on connection timeout + ErrorAsteriskConnTimeout = AMIErrorNew("Asterisk Server connection timeout") + + // ErrorAsteriskInvalidPrompt invalid prompt received from AMI server + ErrorAsteriskInvalidPrompt = AMIErrorNew("Asterisk Server invalid prompt command line") + + // ErrorAsteriskNetwork networking errors + ErrorAsteriskNetwork = AMIErrorNew("Network error") + + // ErrorAsteriskLogin AMI server login failed + ErrorAsteriskLogin = AMIErrorNew("Asterisk Server login failed") + + // Error EOF + ErrorEOF = "EOF" + + // Error I/O + ErrorIO = "io: read/write on closed pipe" + ErrorLoginFailed = "Failed login" +) + +type AMIError struct { + S string + E string +} + +func AMIErrorNew(message string) *AMIError { + return &AMIError{S: message} +} + +func (e *AMIError) AMIError(message string, args ...interface{}) *AMIError { + t := fmt.Sprintf(message, args...) + e.E = fmt.Sprintf(": %s", t) + return e +} + +func (e *AMIError) Error() string { + return fmt.Sprintf("ami has error ocurred: %s%s", e.S, e.E) +} diff --git a/pkg/ami/ami_helper.go b/pkg/ami/ami_helper.go index 40bea45..0c4910f 100644 --- a/pkg/ami/ami_helper.go +++ b/pkg/ami/ami_helper.go @@ -16,7 +16,6 @@ import ( "github.com/nyaruka/phonenumbers" "github.com/pnguyen215/gobase-voip-core/pkg/ami/config" - "github.com/pnguyen215/gobase-voip-core/pkg/ami/fatal" "github.com/pnguyen215/gobase-voip-core/pkg/ami/utils" ) @@ -54,15 +53,15 @@ func OpenDial(ip string, port int) (net.Conn, error) { func OpenDialWith(network, ip string, port int) (net.Conn, error) { if !config.AmiNetworkKeys[network] { - return nil, fatal.AMIErrorNew("AMI: Invalid network") + return nil, AMIErrorNew("AMI: Invalid network") } if ip == "" { - return nil, fatal.AMIErrorNew("AMI: IP must be not empty") + return nil, AMIErrorNew("AMI: IP must be not empty") } if port <= 0 { - return nil, fatal.AMIErrorNew("AMI: Port must be positive number") + return nil, AMIErrorNew("AMI: Port must be positive number") } host, _port, _ := utils.IPDecode(ip) diff --git a/pkg/ami/config/ami_config.go b/pkg/ami/config/ami_config.go index 9dd2a6a..6fd401c 100644 --- a/pkg/ami/config/ami_config.go +++ b/pkg/ami/config/ami_config.go @@ -2,8 +2,6 @@ package config import ( "time" - - "github.com/pnguyen215/gobase-voip-core/pkg/ami/fatal" ) const ( @@ -328,27 +326,6 @@ var ( } ) -var ( - // ErrorAsteriskConnTimeout error on connection timeout - ErrorAsteriskConnTimeout = fatal.AMIErrorNew("Asterisk Server connection timeout") - - // ErrorAsteriskInvalidPrompt invalid prompt received from AMI server - ErrorAsteriskInvalidPrompt = fatal.AMIErrorNew("Asterisk Server invalid prompt command line") - - // ErrorAsteriskNetwork networking errors - ErrorAsteriskNetwork = fatal.AMIErrorNew("Network error") - - // ErrorAsteriskLogin AMI server login failed - ErrorAsteriskLogin = fatal.AMIErrorNew("Asterisk Server login failed") - - // Error EOF - ErrorEOF = "EOF" - - // Error I/O - ErrorIO = "io: read/write on closed pipe" - ErrorLoginFailed = "Failed login" -) - const ( NetworkTimeoutAfterSeconds = time.Second * 3 // default is 3 seconds AmiMaxTimeoutInMsForCall = 100000 // 100000 milliseconds diff --git a/pkg/ami/fatal/ami_fatal.go b/pkg/ami/fatal/ami_fatal.go deleted file mode 100644 index 0035211..0000000 --- a/pkg/ami/fatal/ami_fatal.go +++ /dev/null @@ -1,24 +0,0 @@ -package fatal - -import ( - "fmt" -) - -type AMIError struct { - S string - E string -} - -func AMIErrorNew(message string) *AMIError { - return &AMIError{S: message} -} - -func (e *AMIError) AMIError(message string, args ...interface{}) *AMIError { - t := fmt.Sprintf(message, args...) - e.E = fmt.Sprintf(": %s", t) - return e -} - -func (e *AMIError) Error() string { - return fmt.Sprintf("ami has error ocurred: %s%s", e.S, e.E) -}