/
invalidstatehandler.go
65 lines (48 loc) · 1.97 KB
/
invalidstatehandler.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
package externalapi
import (
"encoding/json"
"net/http"
"github.com/kyma-project/kyma/components/application-registry/internal/httpconsts"
"github.com/kyma-project/kyma/components/application-registry/internal/httperrors"
"github.com/kyma-project/kyma/components/application-registry/internal/httptools"
)
type invalidStateHandler struct {
Message string
}
func NewInvalidStateMetadataHandler(message string) MetadataHandler {
return &invalidStateHandler{Message: message}
}
func (ish *invalidStateHandler) CreateService(w http.ResponseWriter, r *http.Request) {
contextLogger := httptools.ContextLogger(r)
httptools.DumpRequestToLog(r, contextLogger)
ish.HandleRequest(w, r)
}
func (ish *invalidStateHandler) GetService(w http.ResponseWriter, r *http.Request) {
contextLogger := httptools.ContextLoggerWithId(r)
httptools.DumpRequestToLog(r, contextLogger)
ish.HandleRequest(w, r)
}
func (ish *invalidStateHandler) GetServices(w http.ResponseWriter, r *http.Request) {
contextLogger := httptools.ContextLogger(r)
httptools.DumpRequestToLog(r, contextLogger)
ish.HandleRequest(w, r)
}
func (ish *invalidStateHandler) UpdateService(w http.ResponseWriter, r *http.Request) {
contextLogger := httptools.ContextLoggerWithId(r)
httptools.DumpRequestToLog(r, contextLogger)
ish.HandleRequest(w, r)
}
func (ish *invalidStateHandler) DeleteService(w http.ResponseWriter, r *http.Request) {
contextLogger := httptools.ContextLoggerWithId(r)
httptools.DumpRequestToLog(r, contextLogger)
ish.HandleRequest(w, r)
}
func (ish *invalidStateHandler) HandleRequest(w http.ResponseWriter, r *http.Request) {
contextLogger := httptools.ContextLogger(r)
contextLogger.Errorf("Error handling request: %s.", ish.Message)
statusCode := http.StatusInternalServerError
responseBody := httperrors.ErrorResponse{Code: statusCode, Error: ish.Message}
w.Header().Set(httpconsts.HeaderContentType, httpconsts.ContentTypeApplicationJson)
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(responseBody)
}