forked from kyma-project/kyma
/
httperrors.go
34 lines (29 loc) · 866 Bytes
/
httperrors.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
package httperrors
import (
"net/http"
"github.com/kyma-project/kyma/components/application-connector/internal/apperrors"
)
type ErrorResponse struct {
Code int `json:"code"`
Error string `json:"error"`
}
func errorCodeToHttpStatus(code int) int {
switch code {
case apperrors.CodeInternal:
return http.StatusInternalServerError
case apperrors.CodeNotFound:
return http.StatusNotFound
case apperrors.CodeAlreadyExists:
return http.StatusConflict
case apperrors.CodeWrongInput:
return http.StatusBadRequest
case apperrors.CodeUpstreamServerCallFailed:
return http.StatusBadGateway
default:
return http.StatusInternalServerError
}
}
func AppErrorToResponse(appError apperrors.AppError) (status int, body ErrorResponse) {
httpCode := errorCodeToHttpStatus(appError.Code())
return httpCode, ErrorResponse{httpCode, appError.Error()}
}