v0.0.189
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
package ginext
|
||||
|
||||
type apiError struct {
|
||||
ErrorCode string `json:"errorcode"`
|
||||
Message string `json:"message"`
|
||||
FAPIErrorMessage *string `json:"fapiMessage,omitempty"`
|
||||
}
|
||||
|
||||
type extAPIError struct {
|
||||
ErrorCode string `json:"errorcode"`
|
||||
Message string `json:"message"`
|
||||
FAPIErrorMessage *string `json:"fapiMessage,omitempty"`
|
||||
|
||||
RawError *string `json:"__error"`
|
||||
Trace []string `json:"__trace"`
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
package commonApiErr
|
||||
|
||||
type APIErrorCode struct {
|
||||
HTTPStatusCode int
|
||||
Key string
|
||||
}
|
||||
|
||||
//goland:noinspection GoSnakeCaseUsage
|
||||
var (
|
||||
NotImplemented = APIErrorCode{500, "NOT_IMPLEMENTED"}
|
||||
InternalError = APIErrorCode{500, "INTERNAL_ERROR"}
|
||||
Panic = APIErrorCode{500, "PANIC"}
|
||||
|
||||
BindFailURI = APIErrorCode{400, "BINDFAIL_URI"}
|
||||
BindFailQuery = APIErrorCode{400, "BINDFAIL_QUERY"}
|
||||
BindFailJSON = APIErrorCode{400, "BINDFAIL_JSON"}
|
||||
BindFailFormData = APIErrorCode{400, "BINDFAIL_FORMDATA"}
|
||||
|
||||
Unauthorized = APIErrorCode{401, "UNAUTHORIZED"}
|
||||
AuthFailed = APIErrorCode{401, "AUTH_FAILED"}
|
||||
)
|
@@ -10,13 +10,12 @@ type GinWrapper struct {
|
||||
engine *gin.Engine
|
||||
SuppressGinLogs bool
|
||||
|
||||
allowCors bool
|
||||
ginDebug bool
|
||||
returnRawErrors bool
|
||||
requestTimeout time.Duration
|
||||
allowCors bool
|
||||
ginDebug bool
|
||||
requestTimeout time.Duration
|
||||
}
|
||||
|
||||
func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool, timeout time.Duration) *GinWrapper {
|
||||
func NewEngine(allowCors bool, ginDebug bool, timeout time.Duration) *GinWrapper {
|
||||
engine := gin.New()
|
||||
|
||||
wrapper := &GinWrapper{
|
||||
@@ -24,7 +23,6 @@ func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool, timeout time
|
||||
SuppressGinLogs: false,
|
||||
allowCors: allowCors,
|
||||
ginDebug: ginDebug,
|
||||
returnRawErrors: returnRawErrors,
|
||||
requestTimeout: timeout,
|
||||
}
|
||||
|
||||
|
@@ -1,10 +1,9 @@
|
||||
package ginext
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
||||
)
|
||||
|
||||
type WHandlerFunc func(PreContext) HTTPResponse
|
||||
@@ -13,18 +12,20 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
|
||||
|
||||
return func(g *gin.Context) {
|
||||
|
||||
g.Set("__returnRawErrors", w.returnRawErrors)
|
||||
|
||||
reqctx := g.Request.Context()
|
||||
|
||||
wrap, stackTrace, panicObj := callPanicSafe(fn, PreContext{wrapper: w, ginCtx: g})
|
||||
if panicObj != nil {
|
||||
|
||||
fmt.Printf("\n======== ======== STACKTRACE ======== ========\n%s\n======== ======== ======== ========\n\n", stackTrace)
|
||||
log.Error().
|
||||
Interface("panicObj", panicObj).
|
||||
|
||||
err := exerr.
|
||||
New(exerr.TypePanic, "Panic occured (in gin handler)").
|
||||
Any("panicObj", panicObj).
|
||||
Str("trace", stackTrace).
|
||||
Msg("Panic occured (in gin handler)")
|
||||
wrap = APIError(g, commonApiErr.Panic, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v", panicObj)))
|
||||
Build()
|
||||
|
||||
wrap = APIError(g, err)
|
||||
}
|
||||
|
||||
if g.Writer.Written() {
|
||||
|
@@ -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