forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
35 lines (26 loc) · 1016 Bytes
/
response.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
package httphelpers
import (
"encoding/json"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/kyma-project/kyma/components/connector-service/internal/apperrors"
"github.com/kyma-project/kyma/components/connector-service/internal/httpconsts"
"github.com/kyma-project/kyma/components/connector-service/internal/httperrors"
)
func RespondWithErrorAndLog(w http.ResponseWriter, apperr apperrors.AppError) {
log.Errorln(apperr.Error())
RespondWithError(w, apperr)
}
func RespondWithError(w http.ResponseWriter, apperr apperrors.AppError) {
statusCode, responseBody := httperrors.AppErrorToResponse(apperr)
Respond(w, statusCode)
json.NewEncoder(w).Encode(responseBody)
}
func Respond(w http.ResponseWriter, statusCode int) {
w.Header().Set(httpconsts.HeaderContentType, httpconsts.ContentTypeApplicationJson)
w.WriteHeader(statusCode)
}
func RespondWithBody(w http.ResponseWriter, statusCode int, responseBody interface{}) {
Respond(w, statusCode)
json.NewEncoder(w).Encode(responseBody)
}