Compare commits

...

26 Commits

Author SHA1 Message Date
b5f9b6b638 v0.0.211 2023-07-26 10:40:42 +02:00
c7949febf2 v0.0.210 fix ginext route dump 2023-07-25 11:16:11 +02:00
15a4b2a713 v0.0.209 removed g context from err func 2023-07-25 10:56:03 +02:00
493c6ebae8 v0.0.208 remove context from err functions because its not used 2023-07-25 10:51:14 +02:00
fb847b03af v0.0.207 renamed APIError to Error 2023-07-25 10:47:00 +02:00
f826633e6e v0.0.206 2023-07-24 18:50:14 +02:00
edeae23bf1 v0.0.205 2023-07-24 18:47:48 +02:00
a038b86147 v0.0.204 2023-07-24 18:42:33 +02:00
ede0b99d3a v0.0.203 2023-07-24 18:38:04 +02:00
d04ce18eb0 v0.0.202 2023-07-24 18:34:56 +02:00
8ae9a0f107 v0.0.201 2023-07-24 18:22:36 +02:00
a259bb6dbc v0.0.200 2023-07-24 17:42:18 +02:00
adf32568ee v0.0.199 2023-07-24 17:23:38 +02:00
0cfa159cb1 v0.0.198 2023-07-24 14:16:02 +02:00
0ead99608a v0.0.197 2023-07-24 12:27:06 +02:00
7fe3e66cad v0.0.196 2023-07-24 11:47:47 +02:00
a73d7d1654 v0.0.195 2023-07-24 11:42:52 +02:00
bbd7a7bc2c v0.0.194 2023-07-24 11:40:47 +02:00
f5151eb214 v0.0.193 2023-07-24 11:38:57 +02:00
eefb9ac9f5 v0.0.192 2023-07-24 11:30:07 +02:00
468a7d212d v0.0.191 2023-07-24 11:18:25 +02:00
a4def75d06 v0.0.190 2023-07-24 11:16:57 +02:00
16c66ee28c v0.0.189 2023-07-24 11:11:15 +02:00
2e6ca48d22 v0.0.188 exerr MVP 2023-07-24 10:42:39 +02:00
b1d6509294 v0.0.187 forget to use function 2023-07-24 09:16:37 +02:00
e909d656d9 v0.0.186 convert array to interface arr 2023-07-24 09:13:19 +02:00
24 changed files with 1083 additions and 258 deletions

View File

@@ -2,10 +2,13 @@ package exerr
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http" "net/http"
"os" "os"
@@ -17,21 +20,21 @@ import (
// //
// ==== USAGE ===== // ==== USAGE =====
// //
// If some method returns an error _always wrap it into an bmerror: // If some method returns an error _always wrap it into an exerror:
// value, err := do_something(..) // value, err := do_something(..)
// if err != nil { // if err != nil {
// return nil, bmerror.Wrap(err, "do something failed").Build() // return nil, exerror.Wrap(err, "do something failed").Build()
// } // }
// //
// If possible add metadata to the error (eg the id that was not found, ...), the methods are the same as in zerolog // If possible add metadata to the error (eg the id that was not found, ...), the methods are the same as in zerolog
// return nil, bmerror.Wrap(err, "do something failed").Str("someid", id).Int("count", in.Count).Build() // return nil, exerror.Wrap(err, "do something failed").Str("someid", id).Int("count", in.Count).Build()
// //
// You can change the errortype with `.User()` and `.System()` (User-errors are 400 and System-errors 500) // You can change the errortype with `.User()` and `.System()` (User-errors are 400 and System-errors 500)
// You can also manually set the statuscode with `.WithStatuscode(http.NotFound)` // You can also manually set the statuscode with `.WithStatuscode(http.NotFound)`
// You can set the type with `WithType(..)` // You can set the type with `WithType(..)`
// //
// New Errors (that don't wrap an existing err object) are created with New // New Errors (that don't wrap an existing err object) are created with New
// return nil, bmerror.New(bmerror.ErrInternal, "womethign wen horrible wrong").Build() // return nil, exerror.New(exerror.TypeInternal, "womethign wen horrible wrong").Build()
// You can eitehr use an existing ErrorType, the "catch-all" ErrInternal, or add you own ErrType in consts.go // You can eitehr use an existing ErrorType, the "catch-all" ErrInternal, or add you own ErrType in consts.go
// //
// All errors should be handled one of the following four ways: // All errors should be handled one of the following four ways:
@@ -64,37 +67,41 @@ func init() {
} }
type Builder struct { type Builder struct {
bmerror *bringmanError errorData *ExErr
containsGinData bool containsGinData bool
} }
func Get(err error) *Builder { func Get(err error) *Builder {
return &Builder{bmerror: fromError(err)} return &Builder{errorData: FromError(err)}
} }
func New(t ErrorType, msg string) *Builder { func New(t ErrorType, msg string) *Builder {
return &Builder{bmerror: newBringmanErr(CatSystem, t, msg)} return &Builder{errorData: newExErr(CatSystem, t, msg)}
} }
func Wrap(err error, msg string) *Builder { func Wrap(err error, msg string) *Builder {
return &Builder{bmerror: fromError(err).wrap(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)}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
func (b *Builder) WithType(t ErrorType) *Builder { func (b *Builder) WithType(t ErrorType) *Builder {
b.bmerror.Type = t b.errorData.Type = t
return b return b
} }
func (b *Builder) WithStatuscode(status int) *Builder { func (b *Builder) WithStatuscode(status int) *Builder {
b.bmerror.StatusCode = status b.errorData.StatusCode = &status
return b return b
} }
func (b *Builder) WithMessage(msg string) *Builder { func (b *Builder) WithMessage(msg string) *Builder {
b.bmerror.Message = msg b.errorData.Message = msg
return b return b
} }
@@ -119,7 +126,7 @@ func (b *Builder) WithMessage(msg string) *Builder {
// //
// - Send to the error-service // - Send to the error-service
func (b *Builder) Err() *Builder { func (b *Builder) Err() *Builder {
b.bmerror.Severity = SevErr b.errorData.Severity = SevErr
return b return b
} }
@@ -138,7 +145,7 @@ func (b *Builder) Err() *Builder {
// //
// - Logged as Warn // - Logged as Warn
func (b *Builder) Warn() *Builder { func (b *Builder) Warn() *Builder {
b.bmerror.Severity = SevWarn b.errorData.Severity = SevWarn
return b return b
} }
@@ -157,7 +164,7 @@ func (b *Builder) Warn() *Builder {
// //
// - -(nothing)- // - -(nothing)-
func (b *Builder) Info() *Builder { func (b *Builder) Info() *Builder {
b.bmerror.Severity = SevInfo b.errorData.Severity = SevInfo
return b return b
} }
@@ -167,12 +174,12 @@ func (b *Builder) Info() *Builder {
// //
// Errors with category // Errors with category
func (b *Builder) User() *Builder { func (b *Builder) User() *Builder {
b.bmerror.Category = CatUser b.errorData.Category = CatUser
return b return b
} }
func (b *Builder) System() *Builder { func (b *Builder) System() *Builder {
b.bmerror.Category = CatSystem b.errorData.Category = CatSystem
return b return b
} }
@@ -268,7 +275,7 @@ func (b *Builder) Stack() *Builder {
func (b *Builder) Errs(key string, val []error) *Builder { func (b *Builder) Errs(key string, val []error) *Builder {
for i, valerr := range val { for i, valerr := range val {
b.addMeta(fmt.Sprintf("%v[%v]", key, i), MDTString, Get(valerr).toBMError().FormatLog(LogPrintFull)) b.addMeta(fmt.Sprintf("%v[%v]", key, i), MDTString, Get(valerr).errorData.FormatLog(LogPrintFull))
} }
return b return b
} }
@@ -299,7 +306,7 @@ func (b *Builder) GinReq(ctx context.Context, g *gin.Context, req *http.Request)
b.Str("gin.context.reqid", ctxVal) b.Str("gin.context.reqid", ctxVal)
} }
if req.Method != "GET" && req.Body != nil && req.Header.Get("Content-Type") == "application/json" { if req.Method != "GET" && req.Body != nil && req.Header.Get("Content-Type") == "application/json" {
if brc, ok := req.Body.(langext.BufferedReadCloser); ok { if brc, ok := req.Body.(dataext.BufferedReadCloser); ok {
if bin, err := brc.BufferedAll(); err == nil { if bin, err := brc.BufferedAll(); err == nil {
if len(bin) < 16*1024 { if len(bin) < 16*1024 {
var prettyJSON bytes.Buffer var prettyJSON bytes.Buffer
@@ -348,13 +355,17 @@ func formatHeader(header map[string][]string) string {
// Build creates a new error, ready to pass up the stack // Build creates a new error, ready to pass up the stack
// If the errors is not SevWarn or SevInfo it gets also logged (in short form, without stacktrace) onto stdout // If the errors is not SevWarn or SevInfo it gets also logged (in short form, without stacktrace) onto stdout
func (b *Builder) Build() error { func (b *Builder) Build() error {
if b.bmerror.Severity == SevErr || b.bmerror.Severity == SevFatal { warnOnPkgConfigNotInitialized()
b.bmerror.ShortLog(stackSkipLogger.Error())
if pkgconfig.ZeroLogErrTraces && (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) {
b.errorData.ShortLog(stackSkipLogger.Error())
} else if pkgconfig.ZeroLogAllTraces {
b.errorData.ShortLog(stackSkipLogger.Error())
} }
b.CallListener(MethodBuild) b.CallListener(MethodBuild)
return b.bmerror.ToGrpcError() return b.errorData
} }
// Output prints the error onto the gin stdout. // Output prints the error onto the gin stdout.
@@ -366,12 +377,12 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) {
b.GinReq(ctx, g, g.Request) b.GinReq(ctx, g, g.Request)
} }
b.bmerror.Output(ctx, g) b.errorData.Output(g)
if b.bmerror.Severity == SevErr || b.bmerror.Severity == SevFatal { if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal {
b.bmerror.Log(stackSkipLogger.Error()) b.errorData.Log(stackSkipLogger.Error())
} else if b.bmerror.Severity == SevWarn { } else if b.errorData.Severity == SevWarn {
b.bmerror.Log(stackSkipLogger.Warn()) b.errorData.Log(stackSkipLogger.Warn())
} }
b.CallListener(MethodOutput) b.CallListener(MethodOutput)
@@ -380,24 +391,24 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) {
// Print prints the error // Print prints the error
// If the error is SevErr we also send it to the error-service // If the error is SevErr we also send it to the error-service
func (b *Builder) Print() { func (b *Builder) Print() {
if b.bmerror.Severity == SevErr || b.bmerror.Severity == SevFatal { if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal {
b.bmerror.Log(stackSkipLogger.Error()) b.errorData.Log(stackSkipLogger.Error())
} else if b.bmerror.Severity == SevWarn { } else if b.errorData.Severity == SevWarn {
b.bmerror.ShortLog(stackSkipLogger.Warn()) b.errorData.ShortLog(stackSkipLogger.Warn())
} }
b.CallListener(MethodPrint) b.CallListener(MethodPrint)
} }
func (b *Builder) Format(level LogPrintLevel) string { func (b *Builder) Format(level LogPrintLevel) string {
return b.bmerror.FormatLog(level) return b.errorData.FormatLog(level)
} }
// Fatal prints the error and terminates the program // Fatal prints the error and terminates the program
// If the error is SevErr we also send it to the error-service // If the error is SevErr we also send it to the error-service
func (b *Builder) Fatal() { func (b *Builder) Fatal() {
b.bmerror.Severity = SevFatal b.errorData.Severity = SevFatal
b.bmerror.Log(stackSkipLogger.WithLevel(zerolog.FatalLevel)) b.errorData.Log(stackSkipLogger.WithLevel(zerolog.FatalLevel))
b.CallListener(MethodFatal) b.CallListener(MethodFatal)
@@ -407,10 +418,6 @@ func (b *Builder) Fatal() {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
func (b *Builder) addMeta(key string, mdtype metaDataType, val interface{}) *Builder { func (b *Builder) addMeta(key string, mdtype metaDataType, val interface{}) *Builder {
b.bmerror.Meta.add(key, mdtype, val) b.errorData.Meta.add(key, mdtype, val)
return b return b
} }
func (b *Builder) toBMError() BMError {
return b.bmerror.ToBMError()
}

201
exerr/constructor.go Normal file
View File

@@ -0,0 +1,201 @@
package exerr
import (
"encoding/json"
"fmt"
"go.mongodb.org/mongo-driver/bson/primitive"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"reflect"
"time"
)
var reflectTypeStr = reflect.TypeOf("")
func FromError(err error) *ExErr {
if verr, ok := err.(*ExErr); ok {
// A simple ExErr
return verr
}
// A foreign error (eg a MongoDB exception)
return &ExErr{
UniqueID: newID(),
Category: CatForeign,
Type: TypeInternal,
Severity: SevErr,
Timestamp: time.Time{},
StatusCode: nil,
Message: err.Error(),
WrappedErrType: fmt.Sprintf("%T", err),
Caller: "",
OriginalError: nil,
Meta: getForeignMeta(err),
}
}
func newExErr(cat ErrorCategory, errtype ErrorType, msg string) *ExErr {
return &ExErr{
UniqueID: newID(),
Category: cat,
Type: errtype,
Severity: SevErr,
Timestamp: time.Now(),
StatusCode: nil,
Message: msg,
WrappedErrType: "",
Caller: callername(2),
OriginalError: nil,
Meta: make(map[string]MetaValue),
}
}
func wrapExErr(e *ExErr, msg string, cat ErrorCategory, stacktraceskip int) *ExErr {
return &ExErr{
UniqueID: newID(),
Category: cat,
Type: TypeWrap,
Severity: SevErr,
Timestamp: time.Now(),
StatusCode: e.StatusCode,
Message: msg,
WrappedErrType: "",
Caller: callername(1 + stacktraceskip),
OriginalError: e,
Meta: make(map[string]MetaValue),
}
}
func getForeignMeta(err error) (mm MetaMap) {
mm = make(map[string]MetaValue)
defer func() {
if panicerr := recover(); panicerr != nil {
New(TypePanic, "Panic while trying to get foreign meta").
Str("source", err.Error()).
Interface("panic-object", panicerr).
Stack().
Print()
}
}()
rval := reflect.ValueOf(err)
if rval.Kind() == reflect.Interface || rval.Kind() == reflect.Ptr {
rval = reflect.ValueOf(err).Elem()
}
mm.add("foreign.errortype", MDTString, rval.Type().String())
for k, v := range addMetaPrefix("foreign", getReflectedMetaValues(err, 8)) {
mm[k] = v
}
return mm
}
func getReflectedMetaValues(value interface{}, remainingDepth int) map[string]MetaValue {
if remainingDepth <= 0 {
return map[string]MetaValue{}
}
if langext.IsNil(value) {
return map[string]MetaValue{"": {DataType: MDTNil, Value: nil}}
}
rval := reflect.ValueOf(value)
if rval.Type().Kind() == reflect.Ptr {
if rval.IsNil() {
return map[string]MetaValue{"*": {DataType: MDTNil, Value: nil}}
}
elem := rval.Elem()
return addMetaPrefix("*", getReflectedMetaValues(elem.Interface(), remainingDepth-1))
}
if !rval.CanInterface() {
return map[string]MetaValue{"": {DataType: MDTString, Value: "<<no-interface>>"}}
}
raw := rval.Interface()
switch ifraw := raw.(type) {
case time.Time:
return map[string]MetaValue{"": {DataType: MDTTime, Value: ifraw}}
case time.Duration:
return map[string]MetaValue{"": {DataType: MDTDuration, Value: ifraw}}
case int:
return map[string]MetaValue{"": {DataType: MDTInt, Value: ifraw}}
case int8:
return map[string]MetaValue{"": {DataType: MDTInt8, Value: ifraw}}
case int16:
return map[string]MetaValue{"": {DataType: MDTInt16, Value: ifraw}}
case int32:
return map[string]MetaValue{"": {DataType: MDTInt32, Value: ifraw}}
case int64:
return map[string]MetaValue{"": {DataType: MDTInt64, Value: ifraw}}
case string:
return map[string]MetaValue{"": {DataType: MDTString, Value: ifraw}}
case bool:
return map[string]MetaValue{"": {DataType: MDTBool, Value: ifraw}}
case []byte:
return map[string]MetaValue{"": {DataType: MDTBytes, Value: ifraw}}
case float32:
return map[string]MetaValue{"": {DataType: MDTFloat32, Value: ifraw}}
case float64:
return map[string]MetaValue{"": {DataType: MDTFloat64, Value: ifraw}}
case []int:
return map[string]MetaValue{"": {DataType: MDTIntArray, Value: ifraw}}
case []int32:
return map[string]MetaValue{"": {DataType: MDTInt32Array, Value: ifraw}}
case primitive.ObjectID:
return map[string]MetaValue{"": {DataType: MDTObjectID, Value: ifraw}}
case []string:
return map[string]MetaValue{"": {DataType: MDTStringArray, Value: ifraw}}
}
if rval.Type().Kind() == reflect.Struct {
m := make(map[string]MetaValue)
for i := 0; i < rval.NumField(); i++ {
fieldtype := rval.Type().Field(i)
fieldname := fieldtype.Name
if fieldtype.IsExported() {
for k, v := range addMetaPrefix(fieldname, getReflectedMetaValues(rval.Field(i).Interface(), remainingDepth-1)) {
m[k] = v
}
}
}
return m
}
if rval.Type().ConvertibleTo(reflectTypeStr) {
return map[string]MetaValue{"": {DataType: MDTString, Value: rval.Convert(reflectTypeStr).String()}}
}
jsonval, err := json.Marshal(value)
if err != nil {
panic(err) // gets recovered later up
}
return map[string]MetaValue{"": {DataType: MDTString, Value: string(jsonval)}}
}
func addMetaPrefix(prefix string, m map[string]MetaValue) map[string]MetaValue {
if len(m) == 1 {
for k, v := range m {
if k == "" {
return map[string]MetaValue{prefix: v}
}
}
}
r := make(map[string]MetaValue, len(m))
for k, v := range m {
r[prefix+"."+k] = v
}
return r
}

View File

@@ -1,5 +1,9 @@
package exerr package exerr
import (
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)
type ErrorCategory struct{ Category string } type ErrorCategory struct{ Category string }
var ( var (
@@ -9,6 +13,7 @@ var (
CatForeign = ErrorCategory{"Foreign"} // A foreign error that some component threw (e.g. an unknown mongodb error), happens if we call Wrap(..) on an non-bmerror value CatForeign = ErrorCategory{"Foreign"} // A foreign error that some component threw (e.g. an unknown mongodb error), happens if we call Wrap(..) on an non-bmerror value
) )
//goland:noinspection GoUnusedGlobalVariable
var AllCategories = []ErrorCategory{CatWrap, CatSystem, CatUser, CatForeign} var AllCategories = []ErrorCategory{CatWrap, CatSystem, CatUser, CatForeign}
type ErrorSeverity struct{ Severity string } type ErrorSeverity struct{ Severity string }
@@ -22,11 +27,42 @@ var (
SevFatal = ErrorSeverity{"Fatal"} SevFatal = ErrorSeverity{"Fatal"}
) )
//goland:noinspection GoUnusedGlobalVariable
var AllSeverities = []ErrorSeverity{SevTrace, SevDebug, SevInfo, SevWarn, SevErr, SevFatal} var AllSeverities = []ErrorSeverity{SevTrace, SevDebug, SevInfo, SevWarn, SevErr, SevFatal}
type ErrorType struct{ Key string } type ErrorType struct {
Key string
DefaultStatusCode *int
}
//goland:noinspection GoUnusedGlobalVariable
var ( var (
TypeInternal = ErrorType{"Internal"} TypeInternal = ErrorType{"INTERNAL_ERROR", langext.Ptr(500)}
TypePanic = ErrorType{"PANIC", langext.Ptr(500)}
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)}
TypeBindFailHeader = ErrorType{"BINDFAIL_HEADER", 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
) )
func NewType(key string, defStatusCode *int) ErrorType {
return ErrorType{key, defStatusCode}
}
type LogPrintLevel string
const (
LogPrintFull LogPrintLevel = "Full"
LogPrintOverview LogPrintLevel = "Overview"
LogPrintShort LogPrintLevel = "Short"
)

View File

@@ -1,23 +1,37 @@
package exerr package exerr
import (
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)
type ErrorPackageConfig struct { type ErrorPackageConfig struct {
ZeroLogTraces bool // autom print zerolog logs on CreateError ZeroLogErrTraces bool // autom print zerolog logs on .Build() (for SevErr and SevFatal)
RecursiveErrors bool // errors contains their Origin-Error ZeroLogAllTraces bool // autom print zerolog logs on .Build() (for all Severities)
Types []ErrorType // all available error-types RecursiveErrors bool // errors contains their Origin-Error
ExtendedGinOutput bool // Log extended data (trace, meta, ...) to gin in err.Output()
ExtendGinOutput func(err *ExErr, json map[string]any) // (Optionally) extend the gin output with more fields
ExtendGinDataOutput func(err *ExErr, depth int, json map[string]any) // (Optionally) extend the gin `__data` output with more fields
} }
type ErrorPackageConfigInit struct { type ErrorPackageConfigInit struct {
LogTraces bool ZeroLogErrTraces bool
RecursiveErrors bool ZeroLogAllTraces bool
InitTypes func(_ func(_ string) ErrorType) RecursiveErrors bool
ExtendedGinOutput bool
ExtendGinOutput func(err *ExErr, json map[string]any)
ExtendGinDataOutput func(err *ExErr, depth int, json map[string]any)
} }
var initialized = false var initialized = false
var pkgconfig = ErrorPackageConfig{ var pkgconfig = ErrorPackageConfig{
ZeroLogTraces: true, ZeroLogErrTraces: true,
RecursiveErrors: true, ZeroLogAllTraces: false,
Types: []ErrorType{TypeInternal}, RecursiveErrors: true,
ExtendedGinOutput: false,
ExtendGinOutput: func(err *ExErr, json map[string]any) {},
ExtendGinDataOutput: func(err *ExErr, depth int, json map[string]any) {},
} }
// Init initializes the exerr packages // Init initializes the exerr packages
@@ -28,23 +42,35 @@ func Init(cfg ErrorPackageConfigInit) {
panic("Cannot re-init error package") panic("Cannot re-init error package")
} }
types := pkgconfig.Types ego := func(err *ExErr, json map[string]any) {}
egdo := func(err *ExErr, depth int, json map[string]any) {}
fnAddType := func(v string) ErrorType { if cfg.ExtendGinOutput != nil {
et := ErrorType{v} ego = cfg.ExtendGinOutput
types = append(types, et)
return et
} }
if cfg.ExtendGinDataOutput != nil {
if cfg.InitTypes != nil { egdo = cfg.ExtendGinDataOutput
cfg.InitTypes(fnAddType)
} }
pkgconfig = ErrorPackageConfig{ pkgconfig = ErrorPackageConfig{
ZeroLogTraces: cfg.LogTraces, ZeroLogErrTraces: cfg.ZeroLogErrTraces,
RecursiveErrors: cfg.RecursiveErrors, ZeroLogAllTraces: cfg.ZeroLogAllTraces,
Types: types, RecursiveErrors: cfg.RecursiveErrors,
ExtendedGinOutput: cfg.ExtendedGinOutput,
ExtendGinOutput: ego,
ExtendGinDataOutput: egdo,
} }
initialized = true initialized = true
} }
func warnOnPkgConfigNotInitialized() {
if !initialized {
fmt.Printf("\n")
fmt.Printf("%s\n", langext.StrRepeat("=", 80))
fmt.Printf("%s\n", "[WARNING] exerr package used without initializiation")
fmt.Printf("%s\n", " call exerr.Init() in your main() function")
fmt.Printf("%s\n", langext.StrRepeat("=", 80))
fmt.Printf("\n")
}
}

View File

@@ -1,6 +1,10 @@
package exerr package exerr
import ( import (
"github.com/rs/xid"
"github.com/rs/zerolog"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"strings"
"time" "time"
) )
@@ -12,22 +16,188 @@ type ExErr struct {
Severity ErrorSeverity `json:"severity"` Severity ErrorSeverity `json:"severity"`
Type ErrorType `json:"type"` Type ErrorType `json:"type"`
Message string `json:"message"` StatusCode *int `json:"statusCode"`
Caller string `json:"caller"`
Message string `json:"message"`
WrappedErrType string `json:"wrappedErrType"`
Caller string `json:"caller"`
OriginalError *ExErr OriginalError *ExErr
Meta MetaMap `json:"meta"` Meta MetaMap `json:"meta"`
} }
func (ee ExErr) Error() string { func (ee *ExErr) Error() string {
return ee.Message
} }
func (ee ExErr) Unwrap() error { func (ee *ExErr) Unwrap() error {
return ee.OriginalError
} }
func (ee ExErr) Is(err error) bool { func (ee *ExErr) Log(evt *zerolog.Event) {
evt.Msg(ee.FormatLog(LogPrintFull))
}
func (ee *ExErr) FormatLog(lvl LogPrintLevel) string {
if lvl == LogPrintShort {
msg := ee.Message
if ee.OriginalError != nil && ee.OriginalError.Category == CatForeign {
msg = msg + " (" + strings.ReplaceAll(ee.OriginalError.Message, "\n", " ") + ")"
}
if ee.Type != TypeWrap {
return "[" + ee.Type.Key + "] " + msg
} else {
return msg
}
} else if lvl == LogPrintOverview {
str := "[" + ee.RecursiveType().Key + "] <" + ee.UniqueID + "> " + strings.ReplaceAll(ee.RecursiveMessage(), "\n", " ") + "\n"
indent := ""
for curr := ee; curr != nil; curr = curr.OriginalError {
indent += " "
str += indent
str += "-> "
strmsg := strings.Trim(curr.Message, " \r\n\t")
if lbidx := strings.Index(curr.Message, "\n"); lbidx >= 0 {
strmsg = strmsg[0:lbidx]
}
strmsg = langext.StrLimit(strmsg, 61, "...")
str += strmsg
str += "\n"
}
return str
} else if lvl == LogPrintFull {
str := "[" + ee.RecursiveType().Key + "] <" + ee.UniqueID + "> " + strings.ReplaceAll(ee.RecursiveMessage(), "\n", " ") + "\n"
indent := ""
for curr := ee; curr != nil; curr = curr.OriginalError {
indent += " "
etype := ee.Type.Key
if ee.Type == TypeWrap {
etype = "~"
}
str += indent
str += "-> ["
str += etype
if curr.Category == CatForeign {
str += "|Foreign"
}
str += "] "
str += strings.ReplaceAll(curr.Message, "\n", " ")
if curr.Caller != "" {
str += " (@ "
str += curr.Caller
str += ")"
}
str += "\n"
if curr.Meta.Any() {
meta := indent + " {" + curr.Meta.FormatOneLine(240) + "}"
if len(meta) < 200 {
str += meta
str += "\n"
} else {
str += curr.Meta.FormatMultiLine(indent+" ", " ", 1024)
str += "\n"
}
}
}
return str
} else {
return "[?[" + ee.UniqueID + "]?]"
}
}
func (ee *ExErr) ShortLog(evt *zerolog.Event) {
ee.Meta.Apply(evt).Msg(ee.FormatLog(LogPrintShort))
}
// RecursiveMessage returns the message to show
// = first error (top-down) that is not wrapping/foreign/empty
func (ee *ExErr) RecursiveMessage() string {
for curr := ee; curr != nil; curr = curr.OriginalError {
if curr.Message != "" && curr.Category != CatWrap && curr.Category != CatForeign {
return curr.Message
}
}
// fallback to self
return ee.Message
}
// RecursiveType returns the statuscode to use
// = first error (top-down) that is not wrapping/empty
func (ee *ExErr) RecursiveType() ErrorType {
for curr := ee; curr != nil; curr = curr.OriginalError {
if curr.Type != TypeWrap {
return curr.Type
}
}
// fallback to self
return ee.Type
}
// RecursiveStatuscode returns the HTTP Statuscode to use
// = first error (top-down) that has a statuscode set
func (ee *ExErr) RecursiveStatuscode() *int {
for curr := ee; curr != nil; curr = curr.OriginalError {
if curr.StatusCode != nil {
return langext.Ptr(*curr.StatusCode)
}
}
return nil
}
// RecursiveCategory returns the ErrorCategory to use
// = first error (top-down) that has a statuscode set
func (ee *ExErr) RecursiveCategory() ErrorCategory {
for curr := ee; curr != nil; curr = curr.OriginalError {
if curr.Category != CatWrap {
return curr.Category
}
}
// fallback to <empty>
return ee.Category
}
// RecursiveMeta searches (top-down) for teh first error that has a meta value with teh specified key
// and returns its value (or nil)
func (ee *ExErr) RecursiveMeta(key string) *MetaValue {
for curr := ee; curr != nil; curr = curr.OriginalError {
if metaval, ok := curr.Meta[key]; ok {
return langext.Ptr(metaval)
}
}
return nil
}
func (ee *ExErr) Depth() int {
if ee.OriginalError == nil {
return 1
} else {
return ee.OriginalError.Depth() + 1
}
}
func newID() string {
return xid.New().String()
} }

85
exerr/gin.go Normal file
View File

@@ -0,0 +1,85 @@
package exerr
import (
"github.com/gin-gonic/gin"
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"net/http"
"time"
)
func (ee *ExErr) toJson(depth int) gin.H {
ginJson := gin.H{}
if ee.UniqueID != "" {
ginJson["id"] = ee.UniqueID
}
if ee.Category != CatWrap {
ginJson["category"] = ee.Category
}
if ee.Type != TypeWrap {
ginJson["type"] = ee.Type
}
if ee.StatusCode != nil {
ginJson["statuscode"] = ee.StatusCode
}
if ee.Message != "" {
ginJson["message"] = ee.Message
}
if ee.Caller != "" {
ginJson["caller"] = ee.Caller
}
if ee.Severity != SevErr {
ginJson["severity"] = ee.Severity
}
if ee.Timestamp != (time.Time{}) {
ginJson["time"] = ee.Timestamp.Format(time.RFC3339)
}
if ee.WrappedErrType != "" {
ginJson["wrappedErrType"] = ee.WrappedErrType
}
if ee.OriginalError != nil {
ginJson["original"] = ee.OriginalError.toJson(depth + 1)
}
pkgconfig.ExtendGinDataOutput(ee, depth, ginJson)
return ginJson
}
func (ee *ExErr) Output(g *gin.Context) {
var statuscode = http.StatusInternalServerError
var baseCat = ee.RecursiveCategory()
var baseType = ee.RecursiveType()
var baseStatuscode = ee.RecursiveStatuscode()
var baseMessage = ee.RecursiveMessage()
if baseCat == CatUser {
statuscode = http.StatusBadRequest
} else if baseCat == CatSystem {
statuscode = http.StatusInternalServerError
}
if baseStatuscode != nil {
statuscode = *ee.StatusCode
} else if baseType.DefaultStatusCode != nil {
statuscode = *baseType.DefaultStatusCode
}
warnOnPkgConfigNotInitialized()
ginOutput := gin.H{
"errorid": ee.UniqueID,
"message": baseMessage,
"errorcode": baseType.Key,
"category": baseCat.Category,
}
if pkgconfig.ExtendedGinOutput {
ginOutput["__data"] = ee.toJson(0)
}
pkgconfig.ExtendGinOutput(ee, ginOutput)
g.Render(statuscode, json.GoJsonRender{Data: ginOutput, NilSafeSlices: true, NilSafeMaps: true})
}

86
exerr/helper.go Normal file
View File

@@ -0,0 +1,86 @@
package exerr
import "fmt"
// IsType test if the supplied error is of the specified ErrorType.
func IsType(err error, errType ErrorType) bool {
if err == nil {
return false
}
bmerr := FromError(err)
for bmerr != nil {
if bmerr.Type == errType {
return true
}
bmerr = bmerr.OriginalError
}
return false
}
// IsFrom test if the supplied error stems originally from original
func IsFrom(e error, original error) bool {
if e == nil {
return false
}
if e == original {
return true
}
bmerr := FromError(e)
for bmerr == nil {
return false
}
for curr := bmerr; curr != nil; curr = curr.OriginalError {
if curr.Category == CatForeign && curr.Message == original.Error() && curr.WrappedErrType == fmt.Sprintf("%T", original) {
return true
}
}
return false
}
// HasSourceMessage tests if the supplied error stems originally from an error with the message msg
func HasSourceMessage(e error, msg string) bool {
if e == nil {
return false
}
bmerr := FromError(e)
for bmerr == nil {
return false
}
for curr := bmerr; curr != nil; curr = curr.OriginalError {
if curr.OriginalError == nil && curr.Message == msg {
return true
}
}
return false
}
func MessageMatch(e error, matcher func(string) bool) bool {
if e == nil {
return false
}
if matcher(e.Error()) {
return true
}
bmerr := FromError(e)
for bmerr == nil {
return false
}
for curr := bmerr; curr != nil; curr = curr.OriginalError {
if matcher(curr.Message) {
return true
}
}
return false
}

View File

@@ -13,7 +13,7 @@ const (
MethodFatal Method = "FATAL" MethodFatal Method = "FATAL"
) )
type Listener = func(method Method, v ExErr) type Listener = func(method Method, v *ExErr)
var listenerLock = sync.Mutex{} var listenerLock = sync.Mutex{}
var listener = make([]Listener, 0) var listener = make([]Listener, 0)
@@ -26,7 +26,7 @@ func RegisterListener(l Listener) {
} }
func (b *Builder) CallListener(m Method) { func (b *Builder) CallListener(m Method) {
valErr := b.toBMError() valErr := b.errorData
listenerLock.Lock() listenerLock.Lock()
defer listenerLock.Unlock() defer listenerLock.Unlock()

View File

@@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
@@ -15,6 +14,10 @@ import (
"time" "time"
) )
// This is a buffed up map[string]any
// we also save type information of the map-values
// which allows us to deserialize them back into te correct types later
type MetaMap map[string]MetaValue type MetaMap map[string]MetaValue
type metaDataType string type metaDataType string
@@ -350,11 +353,7 @@ func (v *MetaValue) Deserialize(value string, datatype metaDataType) error {
v.DataType = datatype v.DataType = datatype
return nil return nil
} else { } else {
r, err := valueFromProto(value[1:], MDTString) v.Value = langext.Ptr(value[1:])
if err != nil {
return err
}
v.Value = langext.Ptr(r.Value.(string))
v.DataType = datatype v.DataType = datatype
return nil return nil
} }
@@ -586,51 +585,6 @@ func (v MetaValue) ValueString() string {
return "(err)" return "(err)"
} }
func valueFromProto(value string, datatype metaDataType) (MetaValue, error) {
obj := MetaValue{}
err := obj.Deserialize(value, datatype)
if err != nil {
return MetaValue{}, err
}
return obj, nil
}
func metaFromProto(proto []*spbmodels.CustomError_MetaValue) MetaMap {
r := make(MetaMap)
for _, v := range proto {
mval, err := valueFromProto(v.Value, metaDataType(v.Type))
if err != nil {
log.Warn().Err(err).Msg("metaFromProto failed for " + v.Key)
continue
}
r[v.Key] = mval
}
return r
}
func (mm MetaMap) ToProto() []*spbmodels.CustomError_MetaValue {
if mm == nil {
return make([]*spbmodels.CustomError_MetaValue, 0)
}
r := make([]*spbmodels.CustomError_MetaValue, 0, len(mm))
for k, v := range mm {
strval, err := v.SerializeValue()
if err != nil {
log.Warn().Err(err).Msg("MetaMap.ToProto failed for " + k)
continue
}
r = append(r, &spbmodels.CustomError_MetaValue{
Key: k,
Type: string(v.DataType),
Value: strval,
})
}
return r
}
func (mm MetaMap) FormatOneLine(singleMaxLen int) string { func (mm MetaMap) FormatOneLine(singleMaxLen int) string {
r := "" r := ""

14
exerr/stacktrace.go Normal file
View File

@@ -0,0 +1,14 @@
package exerr
import (
"fmt"
"runtime"
)
func callername(skip int) string {
pc := make([]uintptr, 15)
n := runtime.Callers(skip+2, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
return fmt.Sprintf("%s:%d %s", frame.File, frame.Line, frame.Function)
}

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

@@ -50,10 +50,3 @@ func (ac *AppContext) RequestURI() string {
return "" return ""
} }
} }
func (ac *AppContext) FinishSuccess(res HTTPResponse) HTTPResponse {
if ac.cancelled {
panic("Cannot finish a cancelled request")
}
return res
}

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

@@ -1,8 +1,14 @@
package ginext package ginext
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
"net"
"net/http" "net/http"
"strings"
"time" "time"
) )
@@ -10,13 +16,21 @@ 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
routeSpecs []ginRouteSpec
} }
func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool, timeout time.Duration) *GinWrapper { type ginRouteSpec struct {
Method string
URL string
Middlewares []string
Handler string
}
func NewEngine(allowCors bool, ginDebug bool, timeout time.Duration) *GinWrapper {
engine := gin.New() engine := gin.New()
wrapper := &GinWrapper{ wrapper := &GinWrapper{
@@ -24,7 +38,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,
} }
@@ -35,18 +48,94 @@ func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool, timeout time
engine.Use(CorsMiddleware()) engine.Use(CorsMiddleware())
} }
// do not debug-print routes
gin.DebugPrintRouteFunc = func(_, _, _ string, _ int) {}
if ginDebug { if ginDebug {
gin.SetMode(gin.ReleaseMode)
ginlogger := gin.Logger() ginlogger := gin.Logger()
engine.Use(func(context *gin.Context) { engine.Use(func(context *gin.Context) {
if !wrapper.SuppressGinLogs { if !wrapper.SuppressGinLogs {
ginlogger(context) ginlogger(context)
} }
}) })
} else {
gin.SetMode(gin.DebugMode)
} }
return wrapper return wrapper
} }
func (w *GinWrapper) ServeHTTP(writer http.ResponseWriter, request *http.Request) { func (w *GinWrapper) ListenAndServeHTTP(addr string, postInit func(port string)) (chan error, *http.Server) {
w.engine.ServeHTTP(writer, request)
w.DebugPrintRoutes()
httpserver := &http.Server{
Addr: addr,
Handler: w.engine,
}
errChan := make(chan error)
go func() {
ln, err := net.Listen("tcp", httpserver.Addr)
if err != nil {
errChan <- err
return
}
_, port, err := net.SplitHostPort(ln.Addr().String())
if err != nil {
errChan <- err
return
}
log.Info().Str("address", httpserver.Addr).Msg("HTTP-Server started on http://localhost:" + port)
if postInit != nil {
postInit(port) // the net.Listener a few lines above is at this point actually already buffering requests
}
errChan <- httpserver.Serve(ln)
}()
return errChan, httpserver
}
func (w *GinWrapper) DebugPrintRoutes() {
if !w.ginDebug {
return
}
lines := make([][4]string, 0)
pad := [4]int{0, 0, 0, 0}
for _, spec := range w.routeSpecs {
line := [4]string{
spec.Method,
spec.URL,
strings.Join(spec.Middlewares, " -> "),
spec.Handler,
}
lines = append(lines, line)
pad[0] = mathext.Max(pad[0], len(line[0]))
pad[1] = mathext.Max(pad[1], len(line[1]))
pad[2] = mathext.Max(pad[2], len(line[2]))
pad[3] = mathext.Max(pad[3], len(line[3]))
}
for _, line := range lines {
fmt.Printf("Gin-Route: %s %s --> %s --> %s\n",
langext.StrPadRight("["+line[0]+"]", " ", pad[0]+2),
langext.StrPadRight(line[1], " ", pad[1]),
langext.StrPadRight(line[2], " ", pad[2]),
langext.StrPadRight(line[3], " ", pad[3]))
}
} }

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 = Error(err)
} }
if g.Writer.Written() { if g.Writer.Written() {

View File

@@ -2,8 +2,10 @@ package ginext
import ( import (
"context" "context"
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug" "runtime/debug"
) )
@@ -15,6 +17,7 @@ type PreContext struct {
query any query any
body any body any
form any form any
header any
} }
func (pctx *PreContext) URI(uri any) *PreContext { func (pctx *PreContext) URI(uri any) *PreContext {
@@ -37,36 +40,73 @@ func (pctx *PreContext) Form(form any) *PreContext {
return pctx return pctx
} }
func (pctx *PreContext) Header(header any) *PreContext {
pctx.header = header
return pctx
}
func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
if pctx.uri != nil { if pctx.uri != nil {
if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil { if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailURI, "Failed to read uri", err)) err = exerr.Wrap(err, "Failed to read uri").
WithType(exerr.TypeBindFailURI).
Str("struct_type", fmt.Sprintf("%T", pctx.uri)).
Build()
return nil, nil, langext.Ptr(Error(err))
} }
} }
if pctx.query != nil { if pctx.query != nil {
if err := pctx.ginCtx.ShouldBindQuery(pctx.query); err != nil { if err := pctx.ginCtx.ShouldBindQuery(pctx.query); err != nil {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailQuery, "Failed to read query", err)) err = exerr.Wrap(err, "Failed to read query").
WithType(exerr.TypeBindFailQuery).
Str("struct_type", fmt.Sprintf("%T", pctx.query)).
Build()
return nil, nil, langext.Ptr(Error(err))
} }
} }
if pctx.body != nil { if pctx.body != nil {
if pctx.ginCtx.ContentType() == "application/json" { if pctx.ginCtx.ContentType() == "application/json" {
if err := pctx.ginCtx.ShouldBindJSON(pctx.body); err != nil { if err := pctx.ginCtx.ShouldBindJSON(pctx.body); err != nil {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailJSON, "Failed to read body", err)) err = exerr.Wrap(err, "Failed to read json-body").
WithType(exerr.TypeBindFailJSON).
Str("struct_type", fmt.Sprintf("%T", pctx.body)).
Build()
return nil, nil, langext.Ptr(Error(err))
} }
} else { } else {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailJSON, "missing JSON body", nil)) err := exerr.New(exerr.TypeBindFailJSON, "missing JSON body").
Str("struct_type", fmt.Sprintf("%T", pctx.body)).
Build()
return nil, nil, langext.Ptr(Error(err))
} }
} }
if pctx.form != nil { if pctx.form != nil {
if pctx.ginCtx.ContentType() == "multipart/form-data" { if pctx.ginCtx.ContentType() == "multipart/form-data" {
if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil { if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailFormData, "Failed to read multipart-form", err)) err = exerr.Wrap(err, "Failed to read multipart-form").
WithType(exerr.TypeBindFailFormData).
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
Build()
return nil, nil, langext.Ptr(Error(err))
} }
} else { } else {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailJSON, "missing form body", nil)) err := exerr.New(exerr.TypeBindFailFormData, "missing form body").
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
Build()
return nil, nil, langext.Ptr(Error(err))
}
}
if pctx.header != nil {
if err := pctx.ginCtx.ShouldBindHeader(pctx.query); err != nil {
err = exerr.Wrap(err, "Failed to read header").
WithType(exerr.TypeBindFailHeader).
Str("struct_type", fmt.Sprintf("%T", pctx.query)).
Build()
return nil, nil, langext.Ptr(Error(err))
} }
} }

View File

@@ -3,57 +3,97 @@ 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 headerval struct {
Key string
Val string
}
type HTTPResponse interface { type HTTPResponse interface {
Write(g *gin.Context) Write(g *gin.Context)
WithHeader(k string, v string) HTTPResponse
} }
type jsonHTTPResponse struct { type jsonHTTPResponse struct {
statusCode int statusCode int
data any data any
headers []headerval
} }
func (j jsonHTTPResponse) Write(g *gin.Context) { func (j jsonHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
g.Render(j.statusCode, json.GoJsonRender{Data: j.data, NilSafeSlices: true, NilSafeMaps: true}) g.Render(j.statusCode, json.GoJsonRender{Data: j.data, NilSafeSlices: true, NilSafeMaps: true})
} }
func (j jsonHTTPResponse) WithHeader(k string, v string) HTTPResponse {
j.headers = append(j.headers, headerval{k, v})
return j
}
type emptyHTTPResponse struct { type emptyHTTPResponse struct {
statusCode int statusCode int
headers []headerval
} }
func (j emptyHTTPResponse) Write(g *gin.Context) { func (j emptyHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
g.Status(j.statusCode) g.Status(j.statusCode)
} }
func (j emptyHTTPResponse) WithHeader(k string, v string) HTTPResponse {
j.headers = append(j.headers, headerval{k, v})
return j
}
type textHTTPResponse struct { type textHTTPResponse struct {
statusCode int statusCode int
data string data string
headers []headerval
} }
func (j textHTTPResponse) Write(g *gin.Context) { func (j textHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
g.String(j.statusCode, "%s", j.data) g.String(j.statusCode, "%s", j.data)
} }
func (j textHTTPResponse) WithHeader(k string, v string) HTTPResponse {
j.headers = append(j.headers, headerval{k, v})
return j
}
type dataHTTPResponse struct { type dataHTTPResponse struct {
statusCode int statusCode int
data []byte data []byte
contentType string contentType string
headers []headerval
} }
func (j dataHTTPResponse) Write(g *gin.Context) { func (j dataHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
g.Data(j.statusCode, j.contentType, j.data) g.Data(j.statusCode, j.contentType, j.data)
} }
func (j dataHTTPResponse) WithHeader(k string, v string) HTTPResponse {
j.headers = append(j.headers, headerval{k, v})
return j
}
type fileHTTPResponse struct { type fileHTTPResponse struct {
mimetype string mimetype string
filepath string filepath string
filename *string filename *string
headers []headerval
} }
func (j fileHTTPResponse) Write(g *gin.Context) { func (j fileHTTPResponse) Write(g *gin.Context) {
@@ -62,18 +102,46 @@ func (j fileHTTPResponse) Write(g *gin.Context) {
g.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", *j.filename)) g.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", *j.filename))
} }
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
g.File(j.filepath) g.File(j.filepath)
} }
func (j fileHTTPResponse) WithHeader(k string, v string) HTTPResponse {
j.headers = append(j.headers, headerval{k, v})
return j
}
type redirectHTTPResponse struct { type redirectHTTPResponse struct {
statusCode int statusCode int
url string url string
headers []headerval
} }
func (j redirectHTTPResponse) Write(g *gin.Context) { func (j redirectHTTPResponse) Write(g *gin.Context) {
g.Redirect(j.statusCode, j.url) g.Redirect(j.statusCode, j.url)
} }
func (j redirectHTTPResponse) WithHeader(k string, v string) HTTPResponse {
j.headers = append(j.headers, headerval{k, v})
return j
}
type jsonAPIErrResponse struct {
err *exerr.ExErr
headers []headerval
}
func (j jsonAPIErrResponse) Write(g *gin.Context) {
j.err.Output(g)
}
func (j jsonAPIErrResponse) WithHeader(k string, v string) HTTPResponse {
j.headers = append(j.headers, headerval{k, v})
return j
}
func Status(sc int) HTTPResponse { func Status(sc int) HTTPResponse {
return &emptyHTTPResponse{statusCode: sc} return &emptyHTTPResponse{statusCode: sc}
} }
@@ -102,52 +170,18 @@ 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 Error(e error) HTTPResponse {
return createApiError(g, errcode, msg, e) 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,
},
}
} }
} }
func ErrWrap(e error, errorType exerr.ErrorType, msg string) HTTPResponse {
return &jsonAPIErrResponse{
err: exerr.FromError(exerr.Wrap(e, msg).WithType(errorType).Build()),
}
}
func NotImplemented() HTTPResponse {
return Error(exerr.New(exerr.TypeNotImplemented, "").Build())
}

View File

@@ -2,7 +2,14 @@ package ginext
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rext"
"net/http" "net/http"
"path"
"reflect"
"regexp"
"runtime"
"strings"
) )
var anyMethods = []string{ var anyMethods = []string{
@@ -12,60 +19,89 @@ var anyMethods = []string{
} }
type GinRoutesWrapper struct { type GinRoutesWrapper struct {
wrapper *GinWrapper wrapper *GinWrapper
routes gin.IRouter routes gin.IRouter
absPath string
defaultHandler []gin.HandlerFunc
} }
type GinRouteBuilder struct { type GinRouteBuilder struct {
routes *GinRoutesWrapper routes *GinRoutesWrapper
methods []string method string
relPath string relPath string
absPath string
handlers []gin.HandlerFunc handlers []gin.HandlerFunc
} }
func (w *GinWrapper) Routes() *GinRoutesWrapper { func (w *GinWrapper) Routes() *GinRoutesWrapper {
return &GinRoutesWrapper{wrapper: w, routes: w.engine} return &GinRoutesWrapper{
wrapper: w,
routes: w.engine,
absPath: "",
defaultHandler: make([]gin.HandlerFunc, 0),
}
} }
func (w *GinRoutesWrapper) Group(relativePath string) *GinRoutesWrapper { func (w *GinRoutesWrapper) Group(relativePath string) *GinRoutesWrapper {
return &GinRoutesWrapper{wrapper: w.wrapper, routes: w.routes.Group(relativePath)} return &GinRoutesWrapper{
wrapper: w.wrapper,
routes: w.routes.Group(relativePath),
defaultHandler: langext.ArrCopy(w.defaultHandler),
absPath: joinPaths(w.absPath, relativePath),
}
}
func (w *GinRoutesWrapper) Use(middleware ...gin.HandlerFunc) *GinRoutesWrapper {
defHandler := langext.ArrCopy(w.defaultHandler)
defHandler = append(defHandler, middleware...)
return &GinRoutesWrapper{wrapper: w.wrapper, routes: w.routes, defaultHandler: defHandler}
} }
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, methods: []string{http.MethodGet}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return w._route(http.MethodGet, relativePath)
} }
func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, methods: []string{http.MethodPost}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return w._route(http.MethodPost, relativePath)
} }
func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, methods: []string{http.MethodDelete}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return w._route(http.MethodDelete, relativePath)
} }
func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, methods: []string{http.MethodPatch}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return w._route(http.MethodPatch, relativePath)
} }
func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, methods: []string{http.MethodPut}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return w._route(http.MethodPut, relativePath)
} }
func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, methods: []string{http.MethodOptions}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return w._route(http.MethodOptions, relativePath)
} }
func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, methods: []string{http.MethodHead}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return w._route(http.MethodHead, relativePath)
} }
func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, methods: []string{"COUNT"}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return w._route("COUNT", relativePath)
} }
func (w *GinRoutesWrapper) Any(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) Any(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, methods: anyMethods, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return w._route("*", relativePath)
}
func (w *GinRoutesWrapper) _route(method string, relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{
routes: w,
method: method,
relPath: relativePath,
absPath: joinPaths(w.absPath, relativePath),
handlers: langext.ArrCopy(w.defaultHandler),
}
} }
func (w *GinRouteBuilder) Use(middleware ...gin.HandlerFunc) *GinRouteBuilder { func (w *GinRouteBuilder) Use(middleware ...gin.HandlerFunc) *GinRouteBuilder {
@@ -74,12 +110,83 @@ func (w *GinRouteBuilder) Use(middleware ...gin.HandlerFunc) *GinRouteBuilder {
} }
func (w *GinRouteBuilder) Handle(handler WHandlerFunc) { func (w *GinRouteBuilder) Handle(handler WHandlerFunc) {
middlewareNames := langext.ArrMap(w.handlers, func(v gin.HandlerFunc) string { return nameOfFunction(v) })
handlerName := nameOfFunction(handler)
w.handlers = append(w.handlers, Wrap(w.routes.wrapper, handler)) w.handlers = append(w.handlers, Wrap(w.routes.wrapper, handler))
for _, m := range w.methods {
w.routes.routes.Handle(m, w.relPath, w.handlers...) methodName := w.method
if w.method == "*" {
methodName = "ANY"
for _, method := range anyMethods {
w.routes.routes.Handle(method, w.relPath, w.handlers...)
}
} else {
w.routes.routes.Handle(w.method, w.relPath, w.handlers...)
} }
w.routes.wrapper.routeSpecs = append(w.routes.wrapper.routeSpecs, ginRouteSpec{
Method: methodName,
URL: w.absPath,
Middlewares: middlewareNames,
Handler: handlerName,
})
} }
func (w *GinWrapper) NoRoute(handler WHandlerFunc) { func (w *GinWrapper) NoRoute(handler WHandlerFunc) {
w.engine.NoRoute(Wrap(w, handler)) w.engine.NoRoute(Wrap(w, handler))
w.routeSpecs = append(w.routeSpecs, ginRouteSpec{
Method: "ANY",
URL: "[NO_ROUTE]",
Middlewares: nil,
Handler: nameOfFunction(handler),
})
}
func nameOfFunction(f any) string {
fname := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
split := strings.Split(fname, "/")
if len(split) == 0 {
return ""
}
fname = split[len(split)-1]
// https://stackoverflow.com/a/32925345/1761622
if strings.HasSuffix(fname, "-fm") {
fname = fname[:len(fname)-len("-fm")]
}
suffix := rext.W(regexp.MustCompile("\\.func[0-9]+$"))
if match, ok := suffix.MatchFirst(fname); ok {
fname = fname[:len(fname)-match.FullMatch().Length()]
}
return fname
}
// joinPaths is copied verbatim from gin@v1.9.1/gin.go
func joinPaths(absolutePath, relativePath string) string {
if relativePath == "" {
return absolutePath
}
finalPath := path.Join(absolutePath, relativePath)
if lastChar(relativePath) == '/' && lastChar(finalPath) != '/' {
return finalPath + "/"
}
return finalPath
}
func lastChar(str string) uint8 {
if str == "" {
panic("The length of the string can't be 0")
}
return str[len(str)-1]
} }

3
go.mod
View File

@@ -5,6 +5,7 @@ go 1.19
require ( require (
github.com/gin-gonic/gin v1.9.1 github.com/gin-gonic/gin v1.9.1
github.com/jmoiron/sqlx v1.3.5 github.com/jmoiron/sqlx v1.3.5
github.com/rs/xid v1.5.0
github.com/rs/zerolog v1.29.1 github.com/rs/zerolog v1.29.1
go.mongodb.org/mongo-driver v1.12.0 go.mongodb.org/mongo-driver v1.12.0
golang.org/x/crypto v0.11.0 golang.org/x/crypto v0.11.0
@@ -13,7 +14,7 @@ require (
) )
require ( require (
github.com/bytedance/sonic v1.10.0-rc2 // indirect github.com/bytedance/sonic v1.10.0-rc3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect

4
go.sum
View File

@@ -2,6 +2,8 @@ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.0-rc2 h1:oDfRZ+4m6AYCOC0GFeOCeYqvBmucy1isvouS2K0cPzo= github.com/bytedance/sonic v1.10.0-rc2 h1:oDfRZ+4m6AYCOC0GFeOCeYqvBmucy1isvouS2K0cPzo=
github.com/bytedance/sonic v1.10.0-rc2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= github.com/bytedance/sonic v1.10.0-rc2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/bytedance/sonic v1.10.0-rc3 h1:uNSnscRapXTwUgTyOF0GVljYD08p9X/Lbr9MweSV3V0=
github.com/bytedance/sonic v1.10.0-rc3/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
@@ -79,6 +81,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w= github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc= github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=

View File

@@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.185" const GoextVersion = "0.0.211"
const GoextVersionTimestamp = "2023-07-19T19:34:39+0200" const GoextVersionTimestamp = "2023-07-26T10:40:42+0200"

View File

@@ -459,3 +459,11 @@ func ArrExcept[T comparable](arr []T, needles ...T) []T {
} }
return r return r
} }
func ArrayToInterface[T any](t []T) []interface{} {
res := make([]interface{}, 0, len(t))
for i, _ := range t {
res = append(res, t[i])
}
return res
}

View File

@@ -3,6 +3,8 @@ package wmo
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
) )
func (c *Coll[TData]) InsertOne(ctx context.Context, valueIn TData) (TData, error) { func (c *Coll[TData]) InsertOne(ctx context.Context, valueIn TData) (TData, error) {
@@ -15,3 +17,7 @@ func (c *Coll[TData]) InsertOne(ctx context.Context, valueIn TData) (TData, erro
return c.decodeSingle(ctx, mongoRes) return c.decodeSingle(ctx, mongoRes)
} }
func (c *Coll[TData]) InsertMany(ctx context.Context, valueIn []TData) (*mongo.InsertManyResult, error) {
return c.coll.InsertMany(ctx, langext.ArrayToInterface(valueIn))
}