v0.0.189
This commit is contained in:
@@ -3,11 +3,8 @@ package ginext
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
||||
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HTTPResponse interface {
|
||||
@@ -74,6 +71,14 @@ func (j redirectHTTPResponse) Write(g *gin.Context) {
|
||||
g.Redirect(j.statusCode, j.url)
|
||||
}
|
||||
|
||||
type jsonAPIErrResponse struct {
|
||||
err *exerr.ExErr
|
||||
}
|
||||
|
||||
func (j jsonAPIErrResponse) Write(g *gin.Context) {
|
||||
j.err.Output(g)
|
||||
}
|
||||
|
||||
func Status(sc int) HTTPResponse {
|
||||
return &emptyHTTPResponse{statusCode: sc}
|
||||
}
|
||||
@@ -102,52 +107,12 @@ func Redirect(sc int, newURL string) HTTPResponse {
|
||||
return &redirectHTTPResponse{statusCode: sc, url: newURL}
|
||||
}
|
||||
|
||||
func APIError(g *gin.Context, errcode commonApiErr.APIErrorCode, msg string, e error) HTTPResponse {
|
||||
return createApiError(g, errcode, msg, e)
|
||||
func APIError(g *gin.Context, e error) HTTPResponse {
|
||||
return &jsonAPIErrResponse{
|
||||
err: exerr.FromError(e),
|
||||
}
|
||||
}
|
||||
|
||||
func NotImplemented(g *gin.Context) HTTPResponse {
|
||||
return createApiError(g, commonApiErr.NotImplemented, "", nil)
|
||||
}
|
||||
|
||||
func createApiError(g *gin.Context, errcode commonApiErr.APIErrorCode, msg string, e error) HTTPResponse {
|
||||
reqUri := ""
|
||||
if g != nil && g.Request != nil {
|
||||
reqUri = g.Request.Method + " :: " + g.Request.RequestURI
|
||||
}
|
||||
|
||||
log.Error().
|
||||
Str("errorcode.key", errcode.Key).
|
||||
Int("errcode.status", errcode.HTTPStatusCode).
|
||||
Str("uri", reqUri).
|
||||
AnErr("err", e).
|
||||
Stack().
|
||||
Msg(msg)
|
||||
|
||||
var fapiMessage *string = nil
|
||||
if v, ok := e.(interface{ FAPIMessage() string }); ok {
|
||||
fapiMessage = langext.Ptr(v.FAPIMessage())
|
||||
}
|
||||
|
||||
if g.GetBool("__returnRawErrors") {
|
||||
return &jsonHTTPResponse{
|
||||
statusCode: errcode.HTTPStatusCode,
|
||||
data: extAPIError{
|
||||
ErrorCode: errcode.Key,
|
||||
Message: msg,
|
||||
RawError: langext.Ptr(langext.Conditional(e == nil, "", fmt.Sprintf("%+v", e))),
|
||||
FAPIErrorMessage: fapiMessage,
|
||||
Trace: strings.Split(string(debug.Stack()), "\n"),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
return &jsonHTTPResponse{
|
||||
statusCode: errcode.HTTPStatusCode,
|
||||
data: apiError{
|
||||
ErrorCode: errcode.Key,
|
||||
Message: msg,
|
||||
FAPIErrorMessage: fapiMessage,
|
||||
},
|
||||
}
|
||||
}
|
||||
return APIError(g, exerr.New(exerr.TypeNotImplemented, "").Build())
|
||||
}
|
||||
|
Reference in New Issue
Block a user