Skip to content

Commit

Permalink
♻️ refactor: updated codebase #14
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Sep 23, 2023
1 parent ae9a0f5 commit 477d263
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 60 deletions.
18 changes: 9 additions & 9 deletions pkg/ami/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
43 changes: 43 additions & 0 deletions pkg/ami/ami_error.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 3 additions & 4 deletions pkg/ami/ami_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
23 changes: 0 additions & 23 deletions pkg/ami/config/ami_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package config

import (
"time"

"github.com/pnguyen215/gobase-voip-core/pkg/ami/fatal"
)

const (
Expand Down Expand Up @@ -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
Expand Down
24 changes: 0 additions & 24 deletions pkg/ami/fatal/ami_fatal.go

This file was deleted.

0 comments on commit 477d263

Please sign in to comment.