This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
/
common.go
116 lines (101 loc) · 2.75 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package handler
import (
"context"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"strings"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/gin-gonic/gin"
"github.com/keptn/keptn/shipyard-controller/internal/common"
"github.com/keptn/keptn/shipyard-controller/models"
)
//go:generate moq -pkg fake -skip-ensure -out ./fake/eventsender.go . IEventSender
type IEventSender interface {
SendEvent(event cloudevents.Event) error
Send(ctx context.Context, event cloudevents.Event) error
}
// Opaque key type used for graceful shutdown context value
type gracefulShutdownKeyType struct{}
var gracefulShutdownKey = gracefulShutdownKeyType{}
func SetFailedDependencyErrorResponse(c *gin.Context, msg string) {
c.JSON(http.StatusFailedDependency, models.Error{
Code: http.StatusFailedDependency,
Message: &msg,
})
}
func SetNotFoundErrorResponse(c *gin.Context, msg string) {
c.JSON(http.StatusNotFound, models.Error{
Code: http.StatusNotFound,
Message: &msg,
})
}
func SetInternalServerErrorResponse(c *gin.Context, msg string) {
c.JSON(http.StatusInternalServerError, models.Error{
Code: http.StatusInternalServerError,
Message: &msg,
})
}
func SetBadRequestErrorResponse(c *gin.Context, msg string) {
c.JSON(http.StatusBadRequest, models.Error{
Code: http.StatusBadRequest,
Message: &msg,
})
}
func SetConflictErrorResponse(c *gin.Context, msg string) {
c.JSON(http.StatusConflict, models.Error{
Code: http.StatusConflict,
Message: &msg,
})
}
func SetUnprocessableEntityResponse(c *gin.Context, msg string) {
c.JSON(http.StatusUnprocessableEntity, models.Error{
Code: http.StatusUnprocessableEntity,
Message: &msg,
})
}
func DecodeInputData(body io.ReadCloser, params any) error {
jsonData, err := ioutil.ReadAll(body)
if err != nil {
return err
}
d := json.NewDecoder(strings.NewReader(string(jsonData)))
d.DisallowUnknownFields()
if err := d.Decode(¶ms); err != nil {
return err
}
return nil
}
func mapError(c *gin.Context, err error) {
if err == nil {
return
}
if errors.Is(err, common.ErrProjectAlreadyExists) {
SetConflictErrorResponse(c, err.Error())
return
}
if strings.Contains(err.Error(), common.AlreadyInitializedRepositoryMsg) {
SetConflictErrorResponse(c, err.Error())
return
}
if errors.Is(err, common.ErrConfigStoreUpstreamNotFound) {
SetNotFoundErrorResponse(c, err.Error())
return
}
if errors.Is(err, common.ErrConfigStoreInvalidToken) {
SetFailedDependencyErrorResponse(c, err.Error())
return
}
if errors.Is(err, common.ErrProjectNotFound) {
SetNotFoundErrorResponse(c, err.Error())
return
}
if errors.Is(err, common.ErrInvalidStageChange) {
SetBadRequestErrorResponse(c, err.Error())
return
}
SetInternalServerErrorResponse(c, err.Error())
return
}