TP-55555 | Exception handler to push errors to kafka
This commit is contained in:
47
service/ExceptionService.go
Normal file
47
service/ExceptionService.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"cybertron/configs"
|
||||
"cybertron/pkg/encoder"
|
||||
"cybertron/pkg/kafka/producer"
|
||||
"cybertron/pkg/log"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ExceptionService struct {
|
||||
logger *log.Logger
|
||||
dbClient *gorm.DB
|
||||
kafkaProducer producer.KProducer
|
||||
}
|
||||
|
||||
func NewExceptionService(logger *log.Logger, dbClient *gorm.DB, kafkaProducer producer.KProducer) *ExceptionService {
|
||||
return &ExceptionService{
|
||||
logger: logger,
|
||||
dbClient: dbClient,
|
||||
kafkaProducer: kafkaProducer,
|
||||
}
|
||||
}
|
||||
|
||||
func (exceptionService *ExceptionService) CatchErrors(c *gin.Context) {
|
||||
var errorsPayload []interface{}
|
||||
|
||||
if err := c.BindJSON(&errorsPayload); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
|
||||
return
|
||||
}
|
||||
|
||||
headerMap := make(map[string]string)
|
||||
|
||||
for _, errorItem := range errorsPayload {
|
||||
err := exceptionService.kafkaProducer.PublishEvent(errorItem, configs.GetKafkaConfig().GetTopic("js-error-topic"), uuid.NewString(), headerMap, encoder.JsonEncoderInstance)
|
||||
if err != nil {
|
||||
fmt.Println("Failed to push error to kafka")
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
||||
}
|
||||
Reference in New Issue
Block a user