Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
7fe3e66cad
|
|||
a73d7d1654
|
|||
bbd7a7bc2c
|
|||
f5151eb214
|
|||
eefb9ac9f5
|
|||
468a7d212d
|
|||
a4def75d06
|
|||
16c66ee28c
|
@@ -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())
|
||||||
|
@@ -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
|
||||||
|
@@ -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 }
|
||||||
@@ -14,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 }
|
||||||
@@ -27,6 +27,7 @@ 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 {
|
type ErrorType struct {
|
||||||
@@ -34,13 +35,29 @@ type ErrorType struct {
|
|||||||
DefaultStatusCode *int
|
DefaultStatusCode *int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//goland:noinspection GoUnusedGlobalVariable
|
||||||
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
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func NewType(key string, defStatusCode *int) ErrorType {
|
||||||
|
return ErrorType{key, defStatusCode}
|
||||||
|
}
|
||||||
|
|
||||||
type LogPrintLevel string
|
type LogPrintLevel string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@@ -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(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 {
|
||||||
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(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{
|
||||||
ZeroLogErrTraces: true,
|
ZeroLogErrTraces: true,
|
||||||
ZeroLogAllTraces: false,
|
ZeroLogAllTraces: false,
|
||||||
RecursiveErrors: true,
|
RecursiveErrors: true,
|
||||||
ExtendedGinOutput: false,
|
ExtendedGinOutput: false,
|
||||||
Types: []ErrorType{TypeInternal, TypePanic, TypeWrap},
|
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
|
||||||
@@ -39,24 +42,23 @@ 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(key string, defaultStatusCode *int) ErrorType {
|
if cfg.ExtendGinOutput != nil {
|
||||||
et := ErrorType{key, defaultStatusCode}
|
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{
|
||||||
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: ego,
|
||||||
|
ExtendGinDataOutput: egdo,
|
||||||
}
|
}
|
||||||
|
|
||||||
initialized = true
|
initialized = true
|
||||||
|
@@ -162,7 +162,6 @@ func (ee *ExErr) RecursiveStatuscode() *int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fallback to <empty>
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,6 +178,18 @@ func (ee *ExErr) RecursiveCategory() ErrorCategory {
|
|||||||
return ee.Category
|
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 {
|
func (ee *ExErr) Depth() int {
|
||||||
if ee.OriginalError == nil {
|
if ee.OriginalError == nil {
|
||||||
return 1
|
return 1
|
||||||
|
61
exerr/gin.go
61
exerr/gin.go
@@ -1,47 +1,52 @@
|
|||||||
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"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (ee *ExErr) toJson() gin.H {
|
func (ee *ExErr) toJson(depth int) gin.H {
|
||||||
json := gin.H{}
|
ginJson := gin.H{}
|
||||||
|
|
||||||
if ee.UniqueID != "" {
|
if ee.UniqueID != "" {
|
||||||
json["id"] = ee.UniqueID
|
ginJson["id"] = ee.UniqueID
|
||||||
}
|
}
|
||||||
if ee.Category != CatWrap {
|
if ee.Category != CatWrap {
|
||||||
json["category"] = ee.Category
|
ginJson["category"] = ee.Category
|
||||||
}
|
}
|
||||||
if ee.Type != TypeWrap {
|
if ee.Type != TypeWrap {
|
||||||
json["type"] = ee.Type
|
ginJson["type"] = ee.Type
|
||||||
}
|
}
|
||||||
if ee.StatusCode != nil {
|
if ee.StatusCode != nil {
|
||||||
json["statuscode"] = ee.StatusCode
|
ginJson["statuscode"] = ee.StatusCode
|
||||||
}
|
}
|
||||||
if ee.Message != "" {
|
if ee.Message != "" {
|
||||||
json["message"] = ee.Message
|
ginJson["message"] = ee.Message
|
||||||
}
|
}
|
||||||
if ee.Caller != "" {
|
if ee.Caller != "" {
|
||||||
json["caller"] = ee.Caller
|
ginJson["caller"] = ee.Caller
|
||||||
}
|
}
|
||||||
if ee.Severity != SevErr {
|
if ee.Severity != SevErr {
|
||||||
json["severity"] = ee.Severity
|
ginJson["severity"] = ee.Severity
|
||||||
}
|
}
|
||||||
if ee.Timestamp != (time.Time{}) {
|
if ee.Timestamp != (time.Time{}) {
|
||||||
json["time"] = ee.Timestamp.Format(time.RFC3339)
|
ginJson["time"] = ee.Timestamp.Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
if ee.WrappedErrType != "" {
|
||||||
|
ginJson["wrappedErrType"] = ee.WrappedErrType
|
||||||
}
|
}
|
||||||
if ee.OriginalError != nil {
|
if ee.OriginalError != nil {
|
||||||
json["original"] = ee.OriginalError.toJson()
|
ginJson["original"] = ee.OriginalError.toJson(depth + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return json
|
pkgconfig.ExtendGinDataOutput(ee, depth, ginJson)
|
||||||
|
|
||||||
|
return ginJson
|
||||||
}
|
}
|
||||||
|
|
||||||
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(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pkgconfig.ExtendGinOutput(ee, ginOutput)
|
||||||
|
|
||||||
|
g.Render(statuscode, json.GoJsonRender{Data: ginOutput, NilSafeSlices: true, NilSafeMaps: true})
|
||||||
}
|
}
|
||||||
|
@@ -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
|
||||||
}
|
}
|
||||||
|
@@ -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"`
|
|
||||||
}
|
|
@@ -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
|
|
||||||
}
|
|
||||||
|
@@ -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
|
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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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() {
|
||||||
|
@@ -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"
|
||||||
)
|
)
|
||||||
@@ -40,33 +42,55 @@ func (pctx *PreContext) Form(form any) *PreContext {
|
|||||||
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(APIError(pctx.ginCtx, 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(APIError(pctx.ginCtx, 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(APIError(pctx.ginCtx, 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(APIError(pctx.ginCtx, 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(APIError(pctx.ginCtx, 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(APIError(pctx.ginCtx, err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.188"
|
const GoextVersion = "0.0.196"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2023-07-24T10:42:39+0200"
|
const GoextVersionTimestamp = "2023-07-24T11:47:47+0200"
|
||||||
|
Reference in New Issue
Block a user