SimpleCloudNotifier/server/common/ginresp/resp.go

128 lines
3.3 KiB
Go
Raw Normal View History

2022-11-13 19:17:07 +01:00
package ginresp
import (
2022-11-18 21:25:40 +01:00
scn "blackforestbytes.com/simplecloudnotifier"
2022-11-13 19:17:07 +01:00
"blackforestbytes.com/simplecloudnotifier/api/apierr"
2022-11-30 17:58:04 +01:00
"blackforestbytes.com/simplecloudnotifier/api/apihighlight"
2022-11-20 03:06:08 +01:00
"fmt"
2022-11-13 19:17:07 +01:00
"github.com/gin-gonic/gin"
2022-11-20 03:06:08 +01:00
"github.com/rs/zerolog/log"
2022-11-30 21:51:48 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/langext"
2022-11-20 03:06:08 +01:00
"runtime/debug"
2022-11-13 19:17:07 +01:00
)
type HTTPResponse interface {
2022-11-18 21:25:40 +01:00
Write(g *gin.Context)
2022-11-13 19:17:07 +01:00
}
type jsonHTTPResponse struct {
statusCode int
data any
}
func (j jsonHTTPResponse) Write(g *gin.Context) {
g.JSON(j.statusCode, j.data)
}
type emptyHTTPResponse struct {
statusCode int
}
func (j emptyHTTPResponse) Write(g *gin.Context) {
g.Status(j.statusCode)
}
type textHTTPResponse struct {
statusCode int
data string
}
func (j textHTTPResponse) Write(g *gin.Context) {
g.String(j.statusCode, "%s", j.data)
}
type dataHTTPResponse struct {
statusCode int
data []byte
contentType string
}
func (j dataHTTPResponse) Write(g *gin.Context) {
g.Data(j.statusCode, j.contentType, j.data)
}
func Status(sc int) HTTPResponse {
return &emptyHTTPResponse{statusCode: sc}
}
func JSON(sc int, data any) HTTPResponse {
return &jsonHTTPResponse{statusCode: sc, data: data}
}
func Data(sc int, contentType string, data []byte) HTTPResponse {
return &dataHTTPResponse{statusCode: sc, contentType: contentType, data: data}
}
func Text(sc int, data string) HTTPResponse {
return &textHTTPResponse{statusCode: sc, data: data}
}
func InternalError(e error) HTTPResponse {
2022-11-20 17:19:11 +01:00
return createApiError(nil, "InternalError", 500, apierr.INTERNAL_EXCEPTION, 0, e.Error(), e)
2022-11-13 19:17:07 +01:00
}
2022-11-20 20:34:18 +01:00
func APIError(g *gin.Context, status int, errorid apierr.APIError, msg string, e error) HTTPResponse {
return createApiError(g, "APIError", status, errorid, 0, msg, e)
2022-11-13 19:17:07 +01:00
}
2022-11-30 17:58:04 +01:00
func SendAPIError(g *gin.Context, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) HTTPResponse {
2022-11-20 17:19:11 +01:00
return createApiError(g, "SendAPIError", status, errorid, highlight, msg, e)
}
2022-11-20 03:06:08 +01:00
2022-11-20 17:19:11 +01:00
func NotImplemented(g *gin.Context) HTTPResponse {
return createApiError(g, "NotImplemented", 500, apierr.UNDEFINED, 0, "Not Implemented", nil)
2022-11-20 01:28:32 +01:00
}
2022-11-30 17:58:04 +01:00
func createApiError(g *gin.Context, ident string, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) HTTPResponse {
2022-11-20 17:19:11 +01:00
reqUri := ""
if g != nil && g.Request != nil {
reqUri = g.Request.Method + " :: " + g.Request.RequestURI
}
log.Error().
Int("errorid", int(errorid)).
2022-11-30 17:58:04 +01:00
Int("highlight", int(highlight)).
2022-11-20 17:19:11 +01:00
Str("uri", reqUri).
AnErr("err", e).
Stack().
Msg(fmt.Sprintf("[%s] %s", ident, msg))
2022-11-20 03:06:08 +01:00
if scn.Conf.ReturnRawErrors {
2022-11-20 17:19:11 +01:00
return &jsonHTTPResponse{
statusCode: status,
data: apiError{
Success: false,
Error: int(errorid),
2022-11-30 17:58:04 +01:00
ErrorHighlight: int(highlight),
2022-11-20 17:19:11 +01:00
Message: msg,
2022-11-30 21:51:48 +01:00
RawError: langext.Ptr(langext.Conditional(e == nil, "", fmt.Sprintf("%+v", e))),
2022-11-20 17:19:11 +01:00
Trace: string(debug.Stack()),
},
}
2022-11-20 03:06:08 +01:00
} else {
2022-11-20 17:19:11 +01:00
return &jsonHTTPResponse{
statusCode: status,
data: apiError{
Success: false,
Error: int(errorid),
2022-11-30 17:58:04 +01:00
ErrorHighlight: int(highlight),
2022-11-20 17:19:11 +01:00
Message: msg,
},
}
2022-11-20 03:06:08 +01:00
}
2022-11-18 21:25:40 +01:00
}
2022-11-20 17:19:11 +01:00
func CompatAPIError(errid int, msg string) HTTPResponse {
return &jsonHTTPResponse{statusCode: 200, data: compatAPIError{Success: false, ErrorID: errid, Message: msg}}
2022-11-13 19:17:07 +01:00
}