Skip to content

Commit 21fb483

Browse files
Change update error handling for mattermost and teams message delivery (#182)
## What - update error handling for mattermost and teams message delivery ## References <!-- Add identifier for issue tickets, links to other PRs, etc. --> ## Checklist <!-- Remove this section if not applicable to your changes --> - [ ] Tests
1 parent 8edefa3 commit 21fb483

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

pkg/services/notificationchannelservice/mattermostChannelService.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var (
1616
ErrMattermostChannelLimitReached = errors.New("Mattermost channel limit reached.")
1717
ErrListMattermostChannels = errors.New("failed to list mattermost channels")
1818
ErrMattermostChannelNameExists = errors.New("Mattermost channel name already exists.")
19+
ErrMattermostMassageDelivery = errors.New("mattermost message could not be send")
1920
)
2021

2122
type MattermostChannelService interface {
@@ -58,7 +59,11 @@ func (m *mattermostChannelService) SendMattermostTestMessage(webhookUrl string)
5859
}()
5960

6061
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
61-
return fmt.Errorf("failed to send test message to Mattermost webhook: %s", resp.Status)
62+
return fmt.Errorf(
63+
"%w: http status: %s",
64+
ErrMattermostMassageDelivery,
65+
resp.Status,
66+
)
6267
}
6368

6469
return nil

pkg/services/notificationchannelservice/teamsChannelService.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var (
1616
ErrTeamsChannelLimitReached = errors.New("Teams channel limit reached.")
1717
ErrListTeamsChannels = errors.New("failed to list teams channels")
1818
ErrTeamsChannelNameExists = errors.New("Teams channel name already exists.")
19+
ErrTeamsMassageDelivery = errors.New("teams message could not be send")
1920
)
2021

2122
type TeamsChannelService interface {
@@ -58,7 +59,11 @@ func (t *teamsChannelService) SendTeamsTestMessage(webhookUrl string) error {
5859
}()
5960

6061
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
61-
return fmt.Errorf("failed to send test message to Teams webhook: %s", resp.Status)
62+
return fmt.Errorf(
63+
"%w: http status: %s",
64+
ErrTeamsMassageDelivery,
65+
resp.Status,
66+
)
6267
}
6368

6469
return nil

pkg/web/mattermostcontroller/mattermostController.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mattermostcontroller
22

33
import (
4+
"errors"
45
"net/http"
56

67
"github.com/gin-gonic/gin"
@@ -29,11 +30,32 @@ func NewMattermostController(
2930
notificationChannelServicer: notificationChannelServicer,
3031
mattermostChannelService: mattermostChannelService,
3132
}
32-
ctrl.registerRoutes(router, auth)
33+
34+
group := router.Group("/notification-channel/mattermost").
35+
Use(middleware.AuthorizeRoles(auth, "admin")...)
36+
group.POST("", ctrl.CreateMattermostChannel)
37+
group.GET("", ctrl.ListMattermostChannelsByType)
38+
group.PUT("/:id", ctrl.UpdateMattermostChannel)
39+
group.DELETE("/:id", ctrl.DeleteMattermostChannel)
40+
group.POST("/check", ctrl.SendMattermostTestMessage)
41+
42+
router.Use(errorHandler(gin.ErrorTypePrivate))
3343
ctrl.configureMappings(registry)
3444
return ctrl
3545
}
3646

47+
func errorHandler(errorType gin.ErrorType) gin.HandlerFunc {
48+
return func(c *gin.Context) {
49+
c.Next()
50+
for _, errorValue := range c.Errors.ByType(errorType) {
51+
if errors.Is(errorValue, notificationchannelservice.ErrMattermostMassageDelivery) {
52+
c.AbortWithStatusJSON(http.StatusBadRequest, errorResponses.NewErrorGenericResponse(errorValue.Error()))
53+
return
54+
}
55+
}
56+
}
57+
}
58+
3759
func (mc *MattermostController) configureMappings(r *errmap.Registry) {
3860
r.Register(
3961
notificationchannelservice.ErrMattermostChannelLimitReached,
@@ -52,16 +74,6 @@ func (mc *MattermostController) configureMappings(r *errmap.Registry) {
5274
)
5375
}
5476

55-
func (mc *MattermostController) registerRoutes(router gin.IRouter, auth gin.HandlerFunc) {
56-
group := router.Group("/notification-channel/mattermost").
57-
Use(middleware.AuthorizeRoles(auth, "admin")...)
58-
group.POST("", mc.CreateMattermostChannel)
59-
group.GET("", mc.ListMattermostChannelsByType)
60-
group.PUT("/:id", mc.UpdateMattermostChannel)
61-
group.DELETE("/:id", mc.DeleteMattermostChannel)
62-
group.POST("/check", mc.SendMattermostTestMessage)
63-
}
64-
6577
// CreateMattermostChannel
6678
//
6779
// @Summary Create Mattermost Channel

pkg/web/middleware/error_interpreter.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ import (
1414
func InterpretErrors(errorType gin.ErrorType, r errmap.ErrorRegistry) gin.HandlerFunc {
1515
return func(c *gin.Context) {
1616
c.Next()
17-
for _, errorValue := range c.Errors.ByType(errorType) {
1817

18+
if c.IsAborted() {
19+
return
20+
}
21+
22+
for _, errorValue := range c.Errors.ByType(errorType) {
1923
actual := errorValue.Unwrap()
2024

2125
bindingError := ginEx.BindingError{}

pkg/web/teamsController/teamsController.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,23 @@ func AddTeamsController(
4343
group.DELETE("/:id", ctrl.DeleteTeamsChannel)
4444
group.POST("/check", ctrl.SendTeamsTestMessage)
4545

46+
router.Use(errorHandler(gin.ErrorTypePrivate))
4647
ctrl.configureMappings(registry)
4748
return ctrl
4849
}
4950

51+
func errorHandler(errorType gin.ErrorType) gin.HandlerFunc {
52+
return func(c *gin.Context) {
53+
c.Next()
54+
for _, errorValue := range c.Errors.ByType(errorType) {
55+
if errors.Is(errorValue, notificationchannelservice.ErrTeamsMassageDelivery) {
56+
c.AbortWithStatusJSON(http.StatusBadRequest, errorResponses.NewErrorGenericResponse(errorValue.Error()))
57+
return
58+
}
59+
}
60+
}
61+
}
62+
5063
func (tc *TeamsController) configureMappings(r *errmap.Registry) {
5164
r.Register(
5265
notificationchannelservice.ErrTeamsChannelLimitReached,

0 commit comments

Comments
 (0)