Skip to content

Commit

Permalink
Refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Oct 17, 2013
1 parent 270fb12 commit fc0dc7f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
24 changes: 12 additions & 12 deletions go/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import (
"github.com/streadway/amqp"
"log"
"os"
"fmt"
)

func main() {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("Dial: %s", err)
return
log.Fatalf("%s: %s", msg, err)
panic(fmt.Sprintf("%s: %s", msg, err))
}
}

func main() {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()

ch, err := conn.Channel()
if err != nil {
log.Fatalf("Channel: %s", err)
return
}
failOnError(err, "Failed to open a channel")

defer ch.Close()

Expand All @@ -30,12 +32,10 @@ func main() {
false, // noWait
nil, // arguments
)
if err != nil {
log.Fatalf("Queue Declare: %s", err)
return
}
failOnError(err, "Failed to declare a queue")

msgs, err := ch.Consume(q.Name, "", true, false, false, false, nil)
failOnError(err, "Failed to register a consumer")

done := make(chan bool)
var d amqp.Delivery
Expand Down
29 changes: 12 additions & 17 deletions go/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import (
"github.com/streadway/amqp"
"log"
"os"
"fmt"
)

func main() {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("Dial: %s", err)
return
log.Fatalf("%s: %s", msg, err)
panic(fmt.Sprintf("%s: %s", msg, err))
}
}

func main() {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()

ch, err := conn.Channel()
if err != nil {
log.Fatalf("Channel: %s", err)
return
}
failOnError(err, "Failed to open a channel")

defer ch.Close()

Expand All @@ -30,10 +32,7 @@ func main() {
false, // noWait
nil, // arguments
)
if err != nil {
log.Fatalf("Queue Declare: %s", err)
return
}
failOnError(err, "Failed to declare a queue")

body := "hello"
err = ch.Publish(
Expand All @@ -45,11 +44,7 @@ func main() {
ContentType: "text/plain",
Body: []byte(body),
})

if err != nil {
log.Fatalf("Exchange Publish: %s", err)
return
}
failOnError(err, "Failed to publish a message")

os.Exit(0)
}

0 comments on commit fc0dc7f

Please sign in to comment.