This commit is contained in:
Mike Schwörer 2023-07-24 11:11:15 +02:00
parent 2e6ca48d22
commit 16c66ee28c
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
12 changed files with 100 additions and 163 deletions

View File

@ -72,7 +72,7 @@ type Builder struct {
} }
func Get(err error) *Builder { func Get(err error) *Builder {
return &Builder{errorData: fromError(err)} return &Builder{errorData: FromError(err)}
} }
func New(t ErrorType, msg string) *Builder { func New(t ErrorType, msg string) *Builder {
@ -80,7 +80,12 @@ func New(t ErrorType, msg string) *Builder {
} }
func Wrap(err error, msg string) *Builder { func Wrap(err error, msg string) *Builder {
return &Builder{errorData: wrapExErr(fromError(err), msg, CatWrap, 1)} if !pkgconfig.RecursiveErrors {
v := FromError(err)
v.Message = msg
return &Builder{errorData: v}
}
return &Builder{errorData: wrapExErr(FromError(err), msg, CatWrap, 1)}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -372,7 +377,7 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) {
b.GinReq(ctx, g, g.Request) b.GinReq(ctx, g, g.Request)
} }
b.errorData.Output(ctx, g) b.errorData.Output(g)
if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal { if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal {
b.errorData.Log(stackSkipLogger.Error()) b.errorData.Log(stackSkipLogger.Error())

View File

@ -11,7 +11,7 @@ import (
var reflectTypeStr = reflect.TypeOf("") var reflectTypeStr = reflect.TypeOf("")
func fromError(err error) *ExErr { func FromError(err error) *ExErr {
if verr, ok := err.(*ExErr); ok { if verr, ok := err.(*ExErr); ok {
// A simple ExErr // A simple ExErr
return verr return verr

View File

@ -2,7 +2,6 @@ package exerr
import ( import (
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
) )
type ErrorCategory struct{ Category string } type ErrorCategory struct{ Category string }
@ -35,9 +34,20 @@ type ErrorType struct {
} }
var ( var (
TypeInternal = ErrorType{"Internal", langext.Ptr(http.StatusInternalServerError)} TypeInternal = ErrorType{"INTERNAL_ERROR", langext.Ptr(500)}
TypePanic = ErrorType{"Panic", langext.Ptr(http.StatusInternalServerError)} TypePanic = ErrorType{"PANIC", langext.Ptr(500)}
TypeWrap = ErrorType{"Wrap", nil} TypeNotImplemented = ErrorType{"NOT_IMPLEMENTED", langext.Ptr(500)}
TypeWrap = ErrorType{"Wrap", nil}
TypeBindFailURI = ErrorType{"BINDFAIL_URI", langext.Ptr(400)}
TypeBindFailQuery = ErrorType{"BINDFAIL_QUERY", langext.Ptr(400)}
TypeBindFailJSON = ErrorType{"BINDFAIL_JSON", langext.Ptr(400)}
TypeBindFailFormData = ErrorType{"BINDFAIL_FORMDATA", langext.Ptr(400)}
TypeUnauthorized = ErrorType{"UNAUTHORIZED", langext.Ptr(401)}
TypeAuthFailed = ErrorType{"AUTH_FAILED", langext.Ptr(401)}
// other values come from pkgconfig // other values come from pkgconfig
) )

View File

@ -6,29 +6,32 @@ import (
) )
type ErrorPackageConfig struct { type ErrorPackageConfig struct {
ZeroLogErrTraces bool // autom print zerolog logs on .Build() (for SevErr and SevFatal) ZeroLogErrTraces bool // autom print zerolog logs on .Build() (for SevErr and SevFatal)
ZeroLogAllTraces bool // autom print zerolog logs on .Build() (for all Severities) ZeroLogAllTraces bool // autom print zerolog logs on .Build() (for all Severities)
RecursiveErrors bool // errors contains their Origin-Error RecursiveErrors bool // errors contains their Origin-Error
ExtendedGinOutput bool // Log extended data (trace, meta, ...) to gin in err.Output() ExtendedGinOutput bool // Log extended data (trace, meta, ...) to gin in err.Output()
Types []ErrorType // all available error-types ExtendGinOutput func(json map[string]any) // (Optionally) extend the gin output with more fields
ExtendGinDataOutput func(json map[string]any) // (Optionally) extend the gin `__data` output with more fields
} }
type ErrorPackageConfigInit struct { type ErrorPackageConfigInit struct {
ZeroLogErrTraces bool ZeroLogErrTraces bool
ZeroLogAllTraces bool ZeroLogAllTraces bool
RecursiveErrors bool RecursiveErrors bool
ExtendedGinOutput bool ExtendedGinOutput bool
InitTypes func(_ func(key string, defaultStatusCode *int) ErrorType) ExtendGinOutput *func(json map[string]any)
ExtendGinDataOutput *func(json map[string]any)
} }
var initialized = false var initialized = false
var pkgconfig = ErrorPackageConfig{ var pkgconfig = ErrorPackageConfig{
ZeroLogErrTraces: true, ZeroLogErrTraces: true,
ZeroLogAllTraces: false, ZeroLogAllTraces: false,
RecursiveErrors: true, RecursiveErrors: true,
ExtendedGinOutput: false, ExtendedGinOutput: false,
Types: []ErrorType{TypeInternal, TypePanic, TypeWrap}, ExtendGinOutput: func(json map[string]any) {},
ExtendGinDataOutput: func(json map[string]any) {},
} }
// Init initializes the exerr packages // Init initializes the exerr packages
@ -39,24 +42,13 @@ func Init(cfg ErrorPackageConfigInit) {
panic("Cannot re-init error package") panic("Cannot re-init error package")
} }
types := pkgconfig.Types
fnAddType := func(key string, defaultStatusCode *int) ErrorType {
et := ErrorType{key, defaultStatusCode}
types = append(types, et)
return et
}
if cfg.InitTypes != nil {
cfg.InitTypes(fnAddType)
}
pkgconfig = ErrorPackageConfig{ pkgconfig = ErrorPackageConfig{
ZeroLogErrTraces: cfg.ZeroLogErrTraces, ZeroLogErrTraces: cfg.ZeroLogErrTraces,
ZeroLogAllTraces: cfg.ZeroLogAllTraces, ZeroLogAllTraces: cfg.ZeroLogAllTraces,
RecursiveErrors: cfg.RecursiveErrors, RecursiveErrors: cfg.RecursiveErrors,
ExtendedGinOutput: cfg.ExtendedGinOutput, ExtendedGinOutput: cfg.ExtendedGinOutput,
Types: types, ExtendGinOutput: langext.Coalesce(cfg.ExtendGinOutput, func(json map[string]any) {}),
ExtendGinDataOutput: langext.Coalesce(cfg.ExtendGinDataOutput, func(json map[string]any) {}),
} }
initialized = true initialized = true

View File

@ -1,8 +1,8 @@
package exerr package exerr
import ( import (
"context"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"net/http" "net/http"
"time" "time"
) )
@ -34,14 +34,19 @@ func (ee *ExErr) toJson() gin.H {
if ee.Timestamp != (time.Time{}) { if ee.Timestamp != (time.Time{}) {
json["time"] = ee.Timestamp.Format(time.RFC3339) json["time"] = ee.Timestamp.Format(time.RFC3339)
} }
if ee.WrappedErrType != "" {
json["wrappedErrType"] = ee.WrappedErrType
}
if ee.OriginalError != nil { if ee.OriginalError != nil {
json["original"] = ee.OriginalError.toJson() json["original"] = ee.OriginalError.toJson()
} }
pkgconfig.ExtendGinDataOutput(json)
return json return json
} }
func (ee *ExErr) Output(ctx context.Context, g *gin.Context) { func (ee *ExErr) Output(g *gin.Context) {
var statuscode = http.StatusInternalServerError var statuscode = http.StatusInternalServerError
var baseCat = ee.RecursiveCategory() var baseCat = ee.RecursiveCategory()
@ -62,20 +67,18 @@ func (ee *ExErr) Output(ctx context.Context, g *gin.Context) {
warnOnPkgConfigNotInitialized() warnOnPkgConfigNotInitialized()
if pkgconfig.ExtendedGinOutput { ginOutput := gin.H{
g.JSON(statuscode, gin.H{ "errorid": ee.UniqueID,
"errorid": ee.UniqueID, "message": ee.RecursiveMessage(),
"error": ee.RecursiveMessage(), "errorcode": ee.RecursiveType(),
"errorcategory": ee.RecursiveCategory(), "category": ee.RecursiveCategory(),
"errortype": ee.RecursiveType(),
"errodata": ee.toJson(),
})
} else {
g.JSON(statuscode, gin.H{
"errorid": ee.UniqueID,
"error": ee.RecursiveMessage(),
"errorcategory": ee.RecursiveCategory(),
"errortype": ee.RecursiveType(),
})
} }
if pkgconfig.ExtendedGinOutput {
ginOutput["__data"] = ee.toJson()
}
pkgconfig.ExtendGinOutput(ginOutput)
g.Render(statuscode, json.GoJsonRender{Data: ginOutput, NilSafeSlices: true, NilSafeMaps: true})
} }

View File

@ -8,7 +8,7 @@ func IsType(err error, errType ErrorType) bool {
return false return false
} }
bmerr := fromError(err) bmerr := FromError(err)
for bmerr != nil { for bmerr != nil {
if bmerr.Type == errType { if bmerr.Type == errType {
return true return true
@ -28,7 +28,7 @@ func IsFrom(e error, original error) bool {
return true return true
} }
bmerr := fromError(e) bmerr := FromError(e)
for bmerr == nil { for bmerr == nil {
return false return false
} }
@ -48,7 +48,7 @@ func HasSourceMessage(e error, msg string) bool {
return false return false
} }
bmerr := fromError(e) bmerr := FromError(e)
for bmerr == nil { for bmerr == nil {
return false return false
} }
@ -71,7 +71,7 @@ func MessageMatch(e error, matcher func(string) bool) bool {
return true return true
} }
bmerr := fromError(e) bmerr := FromError(e)
for bmerr == nil { for bmerr == nil {
return false return false
} }

View File

@ -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"`
}

View File

@ -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"}
)

View File

@ -10,13 +10,12 @@ type GinWrapper struct {
engine *gin.Engine engine *gin.Engine
SuppressGinLogs bool SuppressGinLogs bool
allowCors bool allowCors bool
ginDebug bool ginDebug bool
returnRawErrors bool requestTimeout time.Duration
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() engine := gin.New()
wrapper := &GinWrapper{ wrapper := &GinWrapper{
@ -24,7 +23,6 @@ func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool, timeout time
SuppressGinLogs: false, SuppressGinLogs: false,
allowCors: allowCors, allowCors: allowCors,
ginDebug: ginDebug, ginDebug: ginDebug,
returnRawErrors: returnRawErrors,
requestTimeout: timeout, requestTimeout: timeout,
} }

View File

@ -1,10 +1,9 @@
package ginext package ginext
import ( import (
"errors"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log" "gogs.mikescher.com/BlackForestBytes/goext/exerr"
) )
type WHandlerFunc func(PreContext) HTTPResponse type WHandlerFunc func(PreContext) HTTPResponse
@ -13,18 +12,20 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
return func(g *gin.Context) { return func(g *gin.Context) {
g.Set("__returnRawErrors", w.returnRawErrors)
reqctx := g.Request.Context() reqctx := g.Request.Context()
wrap, stackTrace, panicObj := callPanicSafe(fn, PreContext{wrapper: w, ginCtx: g}) wrap, stackTrace, panicObj := callPanicSafe(fn, PreContext{wrapper: w, ginCtx: g})
if panicObj != nil { if panicObj != nil {
fmt.Printf("\n======== ======== STACKTRACE ======== ========\n%s\n======== ======== ======== ========\n\n", stackTrace) 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). Str("trace", stackTrace).
Msg("Panic occured (in gin handler)") Build()
wrap = APIError(g, commonApiErr.Panic, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v", panicObj)))
wrap = APIError(g, err)
} }
if g.Writer.Written() { if g.Writer.Written() {

View File

@ -3,11 +3,8 @@ package ginext
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log" "gogs.mikescher.com/BlackForestBytes/goext/exerr"
json "gogs.mikescher.com/BlackForestBytes/goext/gojson" json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug"
"strings"
) )
type HTTPResponse interface { type HTTPResponse interface {
@ -74,6 +71,14 @@ func (j redirectHTTPResponse) Write(g *gin.Context) {
g.Redirect(j.statusCode, j.url) 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 { func Status(sc int) HTTPResponse {
return &emptyHTTPResponse{statusCode: sc} return &emptyHTTPResponse{statusCode: sc}
} }
@ -102,52 +107,12 @@ func Redirect(sc int, newURL string) HTTPResponse {
return &redirectHTTPResponse{statusCode: sc, url: newURL} return &redirectHTTPResponse{statusCode: sc, url: newURL}
} }
func APIError(g *gin.Context, errcode commonApiErr.APIErrorCode, msg string, e error) HTTPResponse { func APIError(g *gin.Context, e error) HTTPResponse {
return createApiError(g, errcode, msg, e) return &jsonAPIErrResponse{
err: exerr.FromError(e),
}
} }
func NotImplemented(g *gin.Context) HTTPResponse { func NotImplemented(g *gin.Context) HTTPResponse {
return createApiError(g, commonApiErr.NotImplemented, "", nil) return APIError(g, exerr.New(exerr.TypeNotImplemented, "").Build())
}
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,
},
}
}
} }

View File

@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.188" const GoextVersion = "0.0.189"
const GoextVersionTimestamp = "2023-07-24T10:42:39+0200" const GoextVersionTimestamp = "2023-07-24T11:11:15+0200"