SimpleCloudNotifier/scnserver/api/ginresp/resp.go

144 lines
3.7 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"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
2023-06-09 21:37:30 +02:00
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
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-12-23 20:27:21 +01:00
"strings"
2022-11-13 19:17:07 +01:00
)
type cookieval struct {
name string
value string
maxAge int
path string
domain string
secure bool
httpOnly bool
2022-11-13 19:17:07 +01:00
}
type headerval struct {
Key string
Val string
2023-01-13 17:17:17 +01:00
}
type errorHTTPResponse struct {
statusCode int
data any
error error
headers []headerval
cookies []cookieval
}
func (j errorHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
for _, v := range j.cookies {
g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly)
}
g.JSON(j.statusCode, j.data)
}
2023-01-13 17:17:17 +01:00
func (j errorHTTPResponse) Statuscode() int {
return j.statusCode
}
func (j errorHTTPResponse) BodyString(g *gin.Context) *string {
2023-01-13 17:17:17 +01:00
v, err := json.Marshal(j.data)
if err != nil {
return nil
}
return langext.Ptr(string(v))
}
func (j errorHTTPResponse) ContentType() string {
return "application/json"
}
func (j errorHTTPResponse) WithHeader(k string, v string) ginext.HTTPResponse {
j.headers = append(j.headers, headerval{k, v})
return j
2022-11-13 19:17:07 +01:00
}
func (j errorHTTPResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) ginext.HTTPResponse {
j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly})
return j
2022-11-13 19:17:07 +01:00
}
func (j errorHTTPResponse) IsSuccess() bool {
return false
2022-11-13 19:17:07 +01:00
}
func (j errorHTTPResponse) Headers() []string {
return langext.ArrMap(j.headers, func(v headerval) string { return v.Key + "=" + v.Val })
2022-11-13 19:17:07 +01:00
}
2024-09-16 15:35:24 +02:00
func (j errorHTTPResponse) Unwrap() error {
return j.error
}
func InternalError(e error) ginext.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
}
func APIError(g *gin.Context, status int, errorid apierr.APIError, msg string, e error) ginext.HTTPResponse {
2022-11-20 20:34:18 +01:00
return createApiError(g, "APIError", status, errorid, 0, msg, e)
2022-11-13 19:17:07 +01:00
}
func SendAPIError(g *gin.Context, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) ginext.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
func createApiError(g *gin.Context, ident string, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) ginext.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 {
return &errorHTTPResponse{
2022-11-20 17:19:11 +01:00
statusCode: status,
2022-12-23 20:27:21 +01:00
data: extendedAPIError{
2022-11-20 17:19:11 +01:00
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-12-23 20:27:21 +01:00
Trace: strings.Split(string(debug.Stack()), "\n"),
2022-11-20 17:19:11 +01:00
},
error: e,
2022-11-20 17:19:11 +01:00
}
2022-11-20 03:06:08 +01:00
} else {
return &errorHTTPResponse{
2022-11-20 17:19:11 +01:00
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,
},
error: e,
2022-11-20 17:19:11 +01:00
}
2022-11-20 03:06:08 +01:00
}
2022-11-18 21:25:40 +01:00
}
func CompatAPIError(errid int, msg string) ginext.HTTPResponse {
return ginext.JSON(200, compatAPIError{Success: false, ErrorID: errid, Message: msg})
2022-11-13 19:17:07 +01:00
}