Skip to content

Commit

Permalink
Clean up error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mercury2269 committed Nov 23, 2018
1 parent 092ca34 commit dcfd0c7
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/fatih/color"
Expand All @@ -20,19 +21,6 @@ var (
region = kingpin.Flag("region", "AWS Region for source and destination queues").Short('r').Default("us-west-2").String()
)

func resolveQueueUrl(svc *sqs.SQS, queueName string) (error, string) {
params := &sqs.GetQueueUrlInput{
QueueName: aws.String(queueName),
}
resp, err := svc.GetQueueUrl(params)

if err != nil {
return err, ""
}

return nil, *resp.QueueUrl
}

func main() {
log.SetHandler(cli.Default)

Expand All @@ -54,9 +42,7 @@ func main() {
err, sourceQueueUrl := resolveQueueUrl(svc, *sourceQueue)

if err != nil {
log.WithError(err).Error(color.New(color.FgRed).Sprintf(
"Unable to locate the source queue with name: %s, check region and name.",
*sourceQueue))
logAwsError("Failed to resolve source queue", err)
return
}

Expand All @@ -65,7 +51,7 @@ func main() {
err, destinationQueueUrl := resolveQueueUrl(svc, *destinationQueue)

if err != nil {
log.Error(color.New(color.FgRed).Sprintf("Unable to locate the destination queue with name: %s, check region and name", *destinationQueue))
logAwsError("Failed to resolve destination queue", err)
return
}

Expand All @@ -78,11 +64,6 @@ func main() {

numberOfMessages, _ := strconv.Atoi(*queueAttributes.Attributes["ApproximateNumberOfMessages"])

if err != nil {
log.Error(color.New(color.FgRed).Sprintf("Unable to locate the destination queue with name: %s, check region and name", *sourceQueue))
return
}

log.Info(color.New(color.FgCyan).Sprintf("Approximate number of messages in the source queue: %s",
*queueAttributes.Attributes["ApproximateNumberOfMessages"]))

Expand All @@ -95,6 +76,27 @@ func main() {

}

func resolveQueueUrl(svc *sqs.SQS, queueName string) (error, string) {
params := &sqs.GetQueueUrlInput{
QueueName: aws.String(queueName),
}
resp, err := svc.GetQueueUrl(params)

if err != nil {
return err, ""
}

return nil, *resp.QueueUrl
}

func logAwsError(message string, err error) {
if awsErr, ok := err.(awserr.Error); ok {
log.Error(color.New(color.FgRed).Sprintf("%s. Error: %s", message, awsErr.Message()))
} else {
log.Error(color.New(color.FgRed).Sprintf("%s. Error: %s", message, err.Error()))
}
}

func convertToEntries(messages []*sqs.Message) []*sqs.SendMessageBatchRequestEntry {
result := make([]*sqs.SendMessageBatchRequestEntry, len(messages))
for i, message := range messages {
Expand Down Expand Up @@ -155,7 +157,7 @@ func moveMessages(sourceQueueUrl string, destinationQueueUrl string, svc *sqs.SQ
}

if err != nil {
log.Error(color.New(color.FgRed).Sprint(err.Error()))
logAwsError("Failed to receive messages", err)
return
}

Expand All @@ -167,12 +169,12 @@ func moveMessages(sourceQueueUrl string, destinationQueueUrl string, svc *sqs.SQ
sendResp, err := svc.SendMessageBatch(batch)

if err != nil {
log.Error(color.New(color.FgRed).Sprintf("Failed to un-queue messages to the destination. Error: %s", err.Error()))
logAwsError("Failed to un-queue messages to the destination", err)
return
}

if len(sendResp.Failed) > 0 {
log.Error(color.New(color.FgRed).Sprint("Failed to un-queue messages to the destination queue."))
log.Error(color.New(color.FgRed).Sprintf("%s messages failed to enqueue, exiting", len(sendResp.Failed)))
return
}

Expand All @@ -185,7 +187,7 @@ func moveMessages(sourceQueueUrl string, destinationQueueUrl string, svc *sqs.SQ
deleteResp, err := svc.DeleteMessageBatch(deleteMessageBatch)

if err != nil {
log.Error(color.New(color.FgRed).Sprint("Error deleting messages, exiting..."))
logAwsError("Failed to delete messages from source queue", err)
return
}

Expand All @@ -201,5 +203,4 @@ func moveMessages(sourceQueueUrl string, destinationQueueUrl string, svc *sqs.SQ
render(b.String())
}


}

0 comments on commit dcfd0c7

Please sign in to comment.