Skip to content

Commit 4b7b083

Browse files
author
root
committed
Util.Err + failWithError
1 parent cb33c00 commit 4b7b083

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

main.go

+35-26
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,68 @@ package main
33
import (
44
"context"
55
"encoding/json"
6-
"strconv"
76
"fmt"
87
"github.com/KittenConnect/rh-api/model"
98
"github.com/KittenConnect/rh-api/util"
109
"github.com/joho/godotenv"
1110
amqp "github.com/rabbitmq/amqp091-go"
1211
"os"
12+
"strconv"
1313
"time"
1414
)
1515

16-
func failOnError(err error, msg string) {
16+
func failWithError(err error, formatString string, args ...any) {
1717
if err != nil {
18-
util.Err(fmt.Errorf("%s: %w", msg, err).Error())
18+
util.Err(fmt.Errorf(fmt.Sprintf("%s: %w",formatString), append(args, err)...).Error())
1919
}
2020
}
2121

2222
var RETRY_DELAY = 5
2323

24-
2524
func main() {
2625
err := godotenv.Load()
27-
failOnError(err, fmt.Sprintf("Error loading .env file : %s", err))
26+
failWithError(err, "Error loading .env file")
2827

2928
conn, err := amqp.Dial(os.Getenv("RABBITMQ_URL"))
30-
failOnError(err, fmt.Sprintf("Failed to connect to broker : %s", err))
29+
failWithError(err, "Failed to connect to broker")
3130

3231
defer conn.Close()
3332

3433
ch, err := conn.Channel()
35-
failOnError(err, fmt.Sprintf("Failed to open a channel : %s", err))
34+
failWithError(err, "Failed to open a channel")
3635

3736
incomingQueue := os.Getenv("RABBITMQ_INCOMING_QUEUE")
38-
outcomingQueue := os.Getenv("RABBITMQ_OUTGOING_QUEUE")
37+
outgoingQueue := os.Getenv("RABBITMQ_OUTGOING_QUEUE")
3938

4039
if value, ok := os.LookupEnv("RABBITMQ_RETRY_DELAY"); ok {
4140
if i, err := strconv.Atoi(value); err == nil {
4241
RETRY_DELAY = i
4342
}
44-
}
43+
}
4544

46-
q, err := ch.QueueDeclare(
45+
inQ, err := ch.QueueDeclare(
4746
incomingQueue,
4847
true,
4948
false,
5049
false,
5150
false,
5251
nil,
5352
)
54-
failOnError(err, fmt.Sprintf("Failed to declare a queue : %s", err))
53+
failWithError(err, "Failed to declare queue %s", incomingQueue)
5554

56-
exchangeArgs := map[string]interface{}{
55+
outQ, err := ch.QueueDeclare(
56+
outgoingQueue,
57+
true,
58+
false,
59+
false,
60+
false,
61+
nil,
62+
)
63+
failWithError(err, "Failed to declare queue %s", outgoingQueue)
64+
65+
exchangeArgs := map[string]interface{}{
5766
"x-delayed-type": "direct",
58-
}
67+
}
5968

6069
err = ch.ExchangeDeclare(
6170
incomingQueue,
@@ -66,32 +75,32 @@ func main() {
6675
false,
6776
exchangeArgs,
6877
)
69-
failOnError(err, fmt.Sprintf("Failed to declare an exchange : %s", err))
78+
failWithError(err, "Failed to declare exchange %s", incomingQueue)
7079

71-
err = ch.QueueBind(
72-
incomingQueue, // queue name
73-
incomingQueue, // routing key
74-
incomingQueue, // exchange
75-
false,
76-
nil)
77-
failOnError(err, fmt.Sprintf("Failed to bind queue: %s", err))
80+
err = ch.QueueBind(
81+
incomingQueue, // queue name
82+
incomingQueue, // routing key
83+
incomingQueue, // exchange
84+
false,
85+
nil)
86+
failWithError(err, "Failed to bind queue %s to exchange %s", incomingQueue, incomingQueue)
7887

7988
// Consommation des messages
8089
msgs, err := ch.Consume(
81-
q.Name, // nom de la queue
90+
inQ.Name, // nom de la queue
8291
"consumer", // consumer
8392
true, // autoAck
8493
false, // exclusive
8594
false, // noLocal
8695
false, // noWait
8796
nil, // arguments
8897
)
89-
failOnError(err, "Failed to register a consumer")
98+
failWithError(err, "Failed to register %s consumer", inQ.Name)
9099
util.Info("Connected to message broker")
91100

92101
netbox := model.NewNetbox()
93102
err = netbox.Connect()
94-
failOnError(err, "Failed to connect to netbox")
103+
failWithError(err, "Failed to connect to netbox")
95104

96105
if netbox.IsConnected() == false {
97106
util.Err("Unable to connect to netbox")
@@ -136,7 +145,7 @@ func main() {
136145
chErr := ch.PublishWithContext(
137146
ctx,
138147
incomingQueue,
139-
q.Name,
148+
inQ.Name,
140149
false,
141150
false,
142151
amqp.Publishing{
@@ -167,7 +176,7 @@ func main() {
167176
chErr := ch.PublishWithContext(
168177
ctx,
169178
"",
170-
outcomingQueue,
179+
outQ.Name,
171180
false,
172181
false,
173182
amqp.Publishing{

util/log.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"os"
77
)
88

9-
func Err(t string) {
9+
func Err(s string, v ...any) {
1010
red := color.New(color.FgRed).SprintFunc()
11-
fmt.Printf("%s %s\n", red("[ERROR]"), red(t))
11+
fmt.Printf(fmt.Sprintf(red("%s %s\n"), "[ERROR]", s), v...)
1212
os.Exit(-1)
1313
}
1414

0 commit comments

Comments
 (0)