Skip to content

Commit

Permalink
Merge pull request #14 from joshmalbrecht/jalbrech/m/errors
Browse files Browse the repository at this point in the history
Improved error messages
  • Loading branch information
joshmalbrecht authored May 12, 2024
2 parents 9c51666 + 361c8c8 commit cd19ddf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
12 changes: 8 additions & 4 deletions internal/queue/sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import (
"github.com/joshmalbrecht/message-sniffer/internal/connection"
)

func Sniff(queueName string, hostname string, port int, virtHost string, username string, password string) {
func Sniff(queueName string, hostname string, port int, virtHost string, username string, password string) error {
conn := connection.Connect(hostname, port, virtHost, username, password)

defer conn.Close()

ch, err := conn.Channel()
if err != nil {
panic(fmt.Sprintf("Unable to connect to channel: %s", err.Error()))
fmt.Println(fmt.Sprintf("Unable to connect to channel: %s", err.Error()))
return err
}

defer ch.Close()

msgs, err := ch.Consume(
queueName, // queue
queueName, // queue name
"", // consumer
false, // auto-ack
false, // exclusive
Expand All @@ -28,7 +29,8 @@ func Sniff(queueName string, hostname string, port int, virtHost string, usernam
nil, // arguments
)
if err != nil {
panic(fmt.Sprintf("Unable to consume message: %s", err.Error()))
fmt.Println(fmt.Sprintf("Unable to consume message: %s", err.Error()))
return err
}

var forever chan struct{}
Expand All @@ -40,4 +42,6 @@ func Sniff(queueName string, hostname string, port int, virtHost string, usernam
}()

<-forever

return nil
}
15 changes: 10 additions & 5 deletions internal/topic/sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"github.com/joshmalbrecht/message-sniffer/internal/connection"
)

func Sniff(exchangeName string, bindingKey string, hostname string, port int, virtHost string, username string, password string) {
func Sniff(exchangeName string, bindingKey string, hostname string, port int, virtHost string, username string, password string) error {
conn := connection.Connect(hostname, port, virtHost, username, password)

defer conn.Close()

ch, err := conn.Channel()
if err != nil {
panic(fmt.Sprintf("Unable to connect to channel: %s", err.Error()))
fmt.Println(fmt.Sprintf("Unable to connect to channel: %s", err.Error()))
return err
}

defer ch.Close()
Expand All @@ -27,7 +28,8 @@ func Sniff(exchangeName string, bindingKey string, hostname string, port int, vi
nil, // arguments
)
if err != nil {
panic(fmt.Sprintf("Unable to declare queue: %s", err.Error()))
fmt.Println(fmt.Sprintf("Unable to declare queue: %s", err.Error()))
return err
}

err = ch.QueueBind(
Expand All @@ -38,11 +40,12 @@ func Sniff(exchangeName string, bindingKey string, hostname string, port int, vi
nil, // arguments
)
if err != nil {
panic(fmt.Sprintf("Unable to bind queue: %s", err.Error()))
fmt.Println(fmt.Sprintf("Unable to bind queue: %s", err.Error()))
return err
}

msgs, err := ch.Consume(
q.Name, // queue
q.Name, // queue name
"", // consumer
true, // auto ack
false, // exclusive
Expand All @@ -60,4 +63,6 @@ func Sniff(exchangeName string, bindingKey string, hostname string, port int, vi
}()

<-forever

return nil
}

0 comments on commit cd19ddf

Please sign in to comment.