From 87777dc95b1923efeb03eff4017c76c105456e5e Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Thu, 23 Aug 2018 15:52:32 +0300 Subject: [PATCH] add ErrorResponse struct --- src/error.go | 15 +++++++++++++++ src/routing.go | 26 +++++++++++++++----------- src/routing_test.go | 4 ++-- src/run.go | 5 ++--- 4 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 src/error.go diff --git a/src/error.go b/src/error.go new file mode 100644 index 0000000..0377d8a --- /dev/null +++ b/src/error.go @@ -0,0 +1,15 @@ +package main + +import ( + "net/http" +) + +type ErrorResponse struct { + Error string `json:"error"` +} + +func BadRequest(error string) (int, interface{}) { + return http.StatusBadRequest, ErrorResponse{ + Error: getLocalizedMessage(error), + } +} diff --git a/src/routing.go b/src/routing.go index 8172040..379ee85 100644 --- a/src/routing.go +++ b/src/routing.go @@ -33,13 +33,13 @@ func addBotHandler(c *gin.Context) { } if cl.ID != 0 { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("bot_already_created")}) + c.AbortWithStatusJSON(BadRequest("bot_already_created")) return } bot, err := tgbotapi.NewBotAPI(b.Token) if err != nil { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("incorrect_token")}) + c.AbortWithStatusJSON(BadRequest("incorrect_token")) logger.Error(b.Token, err.Error()) return } @@ -48,7 +48,7 @@ func addBotHandler(c *gin.Context) { wr, err := bot.SetWebhook(tgbotapi.NewWebhook("https://" + config.HTTPServer.Host + "/telegram/" + bot.Token)) if err != nil || !wr.Ok { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_creating_webhook")}) + c.AbortWithStatusJSON(BadRequest("error_creating_webhook")) logger.Error(b.Token, err.Error(), wr) return } @@ -60,14 +60,14 @@ func addBotHandler(c *gin.Context) { Settings: v1.ChannelSettings{ SpamAllowed: false, Status: v1.Status{ - Delivered: v1.ChannelFeatureNone, + Delivered: v1.ChannelFeatureSend, Read: v1.ChannelFeatureNone, }, Text: v1.ChannelSettingsText{ Creating: v1.ChannelFeatureBoth, Editing: v1.ChannelFeatureBoth, Quoting: v1.ChannelFeatureBoth, - Deleting: v1.ChannelFeatureSend, + Deleting: v1.ChannelFeatureReceive, }, }, } @@ -77,7 +77,7 @@ func addBotHandler(c *gin.Context) { var client = v1.New(conn.MGURL, conn.MGToken) data, status, err := client.ActivateTransportChannel(ch) if status != http.StatusCreated { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_activating_channel")}) + c.AbortWithStatusJSON(BadRequest("error_activating_channel")) logger.Error(conn.APIURL, status, err.Error(), data) return } @@ -97,7 +97,7 @@ func deleteBotHandler(c *gin.Context) { b := c.MustGet("bot").(Bot) conn := getConnectionById(b.ConnectionID) if conn.MGURL == "" || conn.MGToken == "" { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("not_found_account")}) + c.AbortWithStatusJSON(BadRequest("not_found_account")) return } @@ -105,7 +105,7 @@ func deleteBotHandler(c *gin.Context) { data, status, err := client.DeactivateTransportChannel(getBotChannelByToken(b.Token)) if status > http.StatusOK { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_deactivating_channel")}) + c.AbortWithStatusJSON(BadRequest("error_deactivating_channel")) logger.Error(b.ID, status, err.Error(), data) return } @@ -147,7 +147,11 @@ func saveHandler(c *gin.Context) { conn := c.MustGet("connection").(Connection) _, err, code := getAPIClient(conn.APIURL, conn.APIKEY) if err != nil { - c.AbortWithStatusJSON(code, gin.H{"error": err.Error()}) + if code == http.StatusInternalServerError { + c.Error(err) + } else { + c.AbortWithStatusJSON(BadRequest(err.Error())) + } return } @@ -165,7 +169,7 @@ func createHandler(c *gin.Context) { cl := getConnectionByURL(conn.APIURL) if cl.ID != 0 { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("connection_already_created")}) + c.AbortWithStatusJSON(BadRequest("connection_already_created")) return } @@ -187,7 +191,7 @@ func createHandler(c *gin.Context) { } if status >= http.StatusBadRequest { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_activity_mg")}) + c.AbortWithStatusJSON(BadRequest("error_activity_mg")) logger.Error(conn.APIURL, status, errr.ApiErr, data) return } diff --git a/src/routing_test.go b/src/routing_test.go index 43f6226..f6483dc 100644 --- a/src/routing_test.go +++ b/src/routing_test.go @@ -61,14 +61,14 @@ func TestRouting_addBotHandler(t *testing.T) { Settings: v1.ChannelSettings{ SpamAllowed: false, Status: v1.Status{ - Delivered: v1.ChannelFeatureNone, + Delivered: v1.ChannelFeatureSend, Read: v1.ChannelFeatureNone, }, Text: v1.ChannelSettingsText{ Creating: v1.ChannelFeatureBoth, Editing: v1.ChannelFeatureBoth, Quoting: v1.ChannelFeatureBoth, - Deleting: v1.ChannelFeatureSend, + Deleting: v1.ChannelFeatureReceive, }, }, } diff --git a/src/run.go b/src/run.go index a544a09..abf8936 100644 --- a/src/run.go +++ b/src/run.go @@ -1,7 +1,6 @@ package main import ( - "net/http" "os" "os/signal" "regexp" @@ -127,7 +126,7 @@ func checkBotForRequest() gin.HandlerFunc { } if b.Token == "" { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("no_bot_token")}) + c.AbortWithStatusJSON(BadRequest("no_bot_token")) return } @@ -140,7 +139,7 @@ func checkConnectionForRequest() gin.HandlerFunc { var conn Connection if err := c.ShouldBindJSON(&conn); err != nil { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("incorrect_url_key")}) + c.AbortWithStatusJSON(BadRequest("incorrect_url_key")) return }