From 9602d80b4ece3e845677e57a086b50b13613389a Mon Sep 17 00:00:00 2001 From: tursom Date: Thu, 31 Mar 2022 01:22:30 +0800 Subject: [PATCH] update --- exceptions/CollectionOperations.go | 6 +- exceptions/ElementNotFound.go | 8 +-- exceptions/Exception.go | 23 +++--- exceptions/ExceptionConfig.go | 17 +++-- exceptions/IllegalParameter.go | 8 +-- exceptions/IndexOutOfBoundError.go | 10 +-- exceptions/NPE.go | 8 +-- exceptions/OperationNotSupportedException.go | 8 +-- exceptions/PackageException.go | 18 ++++- exceptions/RuntimeException.go | 46 ++++++------ exceptions/TypeCastException.go | 8 +-- exceptions/WrongCallHostException.go | 2 +- main.go | 73 +------------------- 13 files changed, 81 insertions(+), 154 deletions(-) diff --git a/exceptions/CollectionOperations.go b/exceptions/CollectionOperations.go index 20737da..fafa6c3 100644 --- a/exceptions/CollectionOperations.go +++ b/exceptions/CollectionOperations.go @@ -1,5 +1,5 @@ package exceptions -var ElementFound = NewRuntimeException("", "element found", DefaultExceptionConfig().SetGetStackTrace(false)) -var ElementNotFound = NewRuntimeException("", "element not found", DefaultExceptionConfig().SetGetStackTrace(false)) -var CollectionLoopFinished = NewRuntimeException("", "collection loop finished", DefaultExceptionConfig().SetGetStackTrace(false)) +var ElementFound = NewRuntimeException("", DefaultExceptionConfig().SetGetStackTrace(false).SetExceptionName("ElementFound")) +var ElementNotFound = NewRuntimeException("", DefaultExceptionConfig().SetGetStackTrace(false).SetExceptionName("ElementNotFound")) +var CollectionLoopFinished = NewRuntimeException("", DefaultExceptionConfig().SetGetStackTrace(false).SetExceptionName("CollectionLoopFinished")) diff --git a/exceptions/ElementNotFound.go b/exceptions/ElementNotFound.go index 647602a..9ce0e90 100644 --- a/exceptions/ElementNotFound.go +++ b/exceptions/ElementNotFound.go @@ -4,12 +4,8 @@ type ElementNotFoundException struct { RuntimeException } -func NewElementNotFoundException(message any, config *ExceptionConfig) *ElementNotFoundException { +func NewElementNotFoundException(message string, config *ExceptionConfig) *ElementNotFoundException { return &ElementNotFoundException{ - NewRuntimeException( - message, - "exception caused ElementNotFoundException:", - config.AddSkipStack(1), - ), + NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("ElementNotFoundException")), } } diff --git a/exceptions/Exception.go b/exceptions/Exception.go index fde6498..5324c4d 100644 --- a/exceptions/Exception.go +++ b/exceptions/Exception.go @@ -10,7 +10,8 @@ import ( type Exception interface { Cause() Exception Error() string - ErrorMessage() string + Name() string + Message() string StackTrace() []StackTrace PrintStackTrace() PrintStackTraceTo(writer io.Writer) @@ -46,8 +47,8 @@ func BuildStackTraceByArray(builder *strings.Builder, trace []StackTrace) { } } -func BuildStackTrace(builder *strings.Builder, e Exception, exceptionMsg string) { - builder.WriteString(fmt.Sprintln(exceptionMsg, e.ErrorMessage())) +func BuildStackTrace(builder *strings.Builder, e Exception) { + builder.WriteString(fmt.Sprintf("exception caused %s: %s\n", e.Name(), e.Message())) if e.StackTrace() == nil { return } @@ -93,7 +94,7 @@ func Package(err error) Exception { case Exception: return err.(Exception) } - return NewRuntimeException(err, "", DefaultExceptionConfig().SetCause(err)) + return NewRuntimeException("", DefaultExceptionConfig().SetCause(err)) } func PackageAny(err any) Exception { @@ -104,7 +105,7 @@ func PackageAny(err any) Exception { case error: return Package(err.(error)) default: - return NewRuntimeException(err, "", DefaultExceptionConfig()) + return NewRuntimeException("", DefaultExceptionConfig()) } } @@ -114,16 +115,8 @@ func PackagePanic(panic any, exceptionMessage string) Exception { } switch panic.(type) { case error: - return NewRuntimeException( - panic, - exceptionMessage, - DefaultExceptionConfig().SetCause(panic), - ) + return NewRuntimeException("", DefaultExceptionConfig().SetCause(panic)) default: - return NewRuntimeException( - panic, - exceptionMessage, - DefaultExceptionConfig(), - ) + return NewRuntimeException("", DefaultExceptionConfig()) } } diff --git a/exceptions/ExceptionConfig.go b/exceptions/ExceptionConfig.go index b1d7b1a..27737cf 100644 --- a/exceptions/ExceptionConfig.go +++ b/exceptions/ExceptionConfig.go @@ -4,6 +4,7 @@ type ExceptionConfig struct { SkipStack int GetStackTrace bool Cause any + ExceptionName string } func DefaultExceptionConfig() *ExceptionConfig { @@ -16,7 +17,7 @@ func DefaultExceptionConfig() *ExceptionConfig { func (c *ExceptionConfig) SetSkipStack(skipStack int) *ExceptionConfig { if c == nil { - return DefaultExceptionConfig().SetSkipStack(skipStack) + return &ExceptionConfig{SkipStack: skipStack} } c.SkipStack = skipStack return c @@ -24,7 +25,7 @@ func (c *ExceptionConfig) SetSkipStack(skipStack int) *ExceptionConfig { func (c *ExceptionConfig) SetGetStackTrace(getStackTrace bool) *ExceptionConfig { if c == nil { - return DefaultExceptionConfig().SetGetStackTrace(getStackTrace) + return &ExceptionConfig{GetStackTrace: getStackTrace} } c.GetStackTrace = getStackTrace return c @@ -32,7 +33,7 @@ func (c *ExceptionConfig) SetGetStackTrace(getStackTrace bool) *ExceptionConfig func (c *ExceptionConfig) SetCause(cause any) *ExceptionConfig { if c == nil { - return DefaultExceptionConfig().SetCause(cause) + return &ExceptionConfig{Cause: cause} } c.Cause = cause return c @@ -40,8 +41,16 @@ func (c *ExceptionConfig) SetCause(cause any) *ExceptionConfig { func (c *ExceptionConfig) AddSkipStack(skipStack int) *ExceptionConfig { if c == nil { - return DefaultExceptionConfig().AddSkipStack(skipStack) + return &ExceptionConfig{SkipStack: skipStack} } c.SkipStack += skipStack return c } + +func (c *ExceptionConfig) SetExceptionName(exceptionName string) *ExceptionConfig { + if c == nil { + return &ExceptionConfig{ExceptionName: exceptionName} + } + c.ExceptionName = exceptionName + return c +} diff --git a/exceptions/IllegalParameter.go b/exceptions/IllegalParameter.go index 7220ae2..3302545 100644 --- a/exceptions/IllegalParameter.go +++ b/exceptions/IllegalParameter.go @@ -4,12 +4,8 @@ type IllegalParameterException struct { RuntimeException } -func NewIllegalParameterException(message any, config *ExceptionConfig) *IllegalParameterException { +func NewIllegalParameterException(message string, config *ExceptionConfig) *IllegalParameterException { return &IllegalParameterException{ - NewRuntimeException( - message, - "exception caused ElementNotFoundException:", - config.AddSkipStack(1), - ), + NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("IllegalParameterException")), } } diff --git a/exceptions/IndexOutOfBoundError.go b/exceptions/IndexOutOfBoundError.go index 2fd62c7..f33ad66 100644 --- a/exceptions/IndexOutOfBoundError.go +++ b/exceptions/IndexOutOfBoundError.go @@ -4,13 +4,9 @@ type IndexOutOfBound struct { RuntimeException } -func NewIndexOutOfBound(message any, config *ExceptionConfig) *IndexOutOfBound { +func NewIndexOutOfBound(message string, config *ExceptionConfig) *IndexOutOfBound { return &IndexOutOfBound{ - NewRuntimeException( - message, - "exception caused IndexOutOfBound:", - config.AddSkipStack(1), - ), + NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("IndexOutOfBound")), } } @@ -18,7 +14,7 @@ func CatchIndexOutOfBound[T any](f func() T, config *ExceptionConfig) (r T, err defer func() { r := recover() if r != nil { - err = NewIndexOutOfBound(r, config.AddSkipStack(3)) + err = NewIndexOutOfBound("", config.AddSkipStack(3).SetCause(PackageAny(r))) } }() r = f() diff --git a/exceptions/NPE.go b/exceptions/NPE.go index d5f72ff..728b5f0 100644 --- a/exceptions/NPE.go +++ b/exceptions/NPE.go @@ -9,13 +9,9 @@ type NPE struct { RuntimeException } -func NewNPE(message any, config *ExceptionConfig) *NPE { +func NewNPE(message string, config *ExceptionConfig) *NPE { return &NPE{ - NewRuntimeException( - message, - "exception caused NullPointerException:", - config.AddSkipStack(1), - ), + NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("NPE")), } } diff --git a/exceptions/OperationNotSupportedException.go b/exceptions/OperationNotSupportedException.go index 4b0bdc5..9cd86a2 100644 --- a/exceptions/OperationNotSupportedException.go +++ b/exceptions/OperationNotSupportedException.go @@ -4,12 +4,8 @@ type OperationNotSupportedException struct { RuntimeException } -func NewOperationNotSupportedException(message any, config *ExceptionConfig) *OperationNotSupportedException { +func NewOperationNotSupportedException(message string, config *ExceptionConfig) *OperationNotSupportedException { return &OperationNotSupportedException{ - NewRuntimeException( - message, - "exception caused OperationNotSupportedException:", - config.AddSkipStack(1), - ), + NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("OperationNotSupportedException")), } } diff --git a/exceptions/PackageException.go b/exceptions/PackageException.go index 3aa7237..dac9b45 100644 --- a/exceptions/PackageException.go +++ b/exceptions/PackageException.go @@ -1,13 +1,27 @@ package exceptions +import ( + "fmt" + "reflect" +) + type PackageException struct { RuntimeException err any } -func NewPackageException(err any, exceptionMessage string, config *ExceptionConfig) *PackageException { +func NewPackageException(err any, config *ExceptionConfig) *PackageException { + message := "" + switch e := err.(type) { + case error: + message = e.Error() + default: + message = fmt.Sprint(e) + } + t := reflect.TypeOf(err) + message = fmt.Sprintf("%s (%s)", message, t.Name()) return &PackageException{ - RuntimeException: NewRuntimeException(err, exceptionMessage, config.AddSkipStack(1)), + RuntimeException: NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("PackageException")), err: err, } } diff --git a/exceptions/RuntimeException.go b/exceptions/RuntimeException.go index 15af254..6ad0039 100644 --- a/exceptions/RuntimeException.go +++ b/exceptions/RuntimeException.go @@ -1,7 +1,6 @@ package exceptions import ( - "fmt" "github.com/tursom/GoCollections/lang" "io" "os" @@ -10,13 +9,13 @@ import ( type RuntimeException struct { lang.BaseObject - message string - exceptionMessage string - stackTrace []StackTrace - cause Exception + message string + exceptionName string + stackTrace []StackTrace + cause Exception } -func NewRuntimeException(message any, exceptionMessage string, config *ExceptionConfig) RuntimeException { +func NewRuntimeException(message string, config *ExceptionConfig) RuntimeException { if config == nil { config = DefaultExceptionConfig() } @@ -26,27 +25,28 @@ func NewRuntimeException(message any, exceptionMessage string, config *Exception stackTrace = GetStackTraceSkipDeep(config.SkipStack + 1) } - if len(exceptionMessage) == 0 { - exceptionMessage = "exception caused:" - } - var causeException Exception = nil if config.Cause != nil { - switch config.Cause.(type) { + switch e := config.Cause.(type) { case Exception: - causeException = config.Cause.(Exception) + causeException = e default: - causeException = NewPackageException(config.Cause, "exception caused:", DefaultExceptionConfig(). + causeException = NewPackageException(config.Cause, DefaultExceptionConfig(). SetGetStackTrace(false)) } } + exceptionName := "RuntimeException" + if len(config.ExceptionName) != 0 { + exceptionName = config.ExceptionName + } + return RuntimeException{ - BaseObject: lang.NewBaseObject(), - message: fmt.Sprint(message), - exceptionMessage: exceptionMessage, - stackTrace: stackTrace, - cause: causeException, + BaseObject: lang.NewBaseObject(), + message: message, + stackTrace: stackTrace, + cause: causeException, + exceptionName: exceptionName, } } @@ -60,8 +60,12 @@ func (o RuntimeException) Error() string { return builder.String() } -func (o RuntimeException) ErrorMessage() string { - return o.exceptionMessage +func (o RuntimeException) Message() string { + return o.message +} + +func (o RuntimeException) Name() string { + return o.exceptionName } func (o RuntimeException) StackTrace() []StackTrace { @@ -86,7 +90,7 @@ func (o RuntimeException) PrintStackTraceTo(writer io.Writer) { } func (o RuntimeException) BuildPrintStackTrace(builder *strings.Builder) { - BuildStackTrace(builder, o, o.exceptionMessage) + BuildStackTrace(builder, o) if o.cause != nil { builder.WriteString("caused by: ") o.cause.BuildPrintStackTrace(builder) diff --git a/exceptions/TypeCastException.go b/exceptions/TypeCastException.go index cb97f15..2256e33 100644 --- a/exceptions/TypeCastException.go +++ b/exceptions/TypeCastException.go @@ -10,13 +10,9 @@ type TypeCastException struct { RuntimeException } -func NewTypeCastException(message any, config *ExceptionConfig) *TypeCastException { +func NewTypeCastException(message string, config *ExceptionConfig) *TypeCastException { return &TypeCastException{ - NewRuntimeException( - message, - "exception caused ElementNotFoundException:", - config.AddSkipStack(1), - ), + NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("TypeCastException")), } } diff --git a/exceptions/WrongCallHostException.go b/exceptions/WrongCallHostException.go index cd71965..5c1602d 100644 --- a/exceptions/WrongCallHostException.go +++ b/exceptions/WrongCallHostException.go @@ -6,6 +6,6 @@ type WrongCallHostException struct { func NewWrongCallHostException(message string) WrongCallHostException { return WrongCallHostException{ - NewRuntimeException(nil, message, DefaultExceptionConfig().AddSkipStack(1)), + NewRuntimeException(message, DefaultExceptionConfig().AddSkipStack(1).SetExceptionName("WrongCallHostException")), } } diff --git a/main.go b/main.go index 7e057a7..5bb9245 100644 --- a/main.go +++ b/main.go @@ -1,76 +1,7 @@ package main -import ( - "fmt" - "github.com/tursom/GoCollections/collections" - "github.com/tursom/GoCollections/exceptions" - "github.com/tursom/GoCollections/lang" - "time" -) +import "github.com/tursom/GoCollections/exceptions" func main() { - _, err := exceptions.Try(func() (any, exceptions.Exception) { - panic("test") - }, func(r any) (any, exceptions.Exception) { - fmt.Println("recover from panic", r) - return nil, exceptions.NewIndexOutOfBound(fmt.Sprint(r), nil) - }) - exceptions.Print(err) - - list := collections.NewConcurrentLinkedQueue[lang.Int]() - target := collections.NewArrayListByCapacity[lang.Int](10000) - fmt.Println("list", list) - - go func() { - for i := 0; i < 1000000; i++ { - list.Offer() - //fmt.Println(offer) - //if element != nil { - // target.Add(element) - //} - } - fmt.Println("target:", target) - }() - - go func() { - for i := 0; i < 1000; i++ { - err = list.Push(lang.Int(i)) - //fmt.Println(err) - } - time.Sleep(time.Second * 2) - fmt.Println(list) - }() - // - //for i := 0; i < 100; i++ { - // fmt.Println(list) - //} - time.Sleep(time.Second * 10) - fmt.Println("target:", target) - - //for i := 0; i < 20; i++ { - // list.Push(i) - // fmt.Println(list) - //} - // - //for i := 0; i < 20; i++ { - // list.Offer() - // fmt.Println(list) - //} - // - //for i := 0; i < 25; i++ { - // list.Push(i) - // fmt.Println(list) - //} - // - //_ = collections.LoopMutable(list, func(element any, iterator collections.MutableIterator) (err exceptions.Exception) { - // if element.(int)&1 == 0 { - // err = iterator.Remove() - // } - // fmt.Println(list) - // return - //}) - //for i := 0; i < 10; i++ { - // list.Remove(i * 2) - // fmt.Println(list) - //} + exceptions.NewRuntimeException("test2", exceptions.DefaultExceptionConfig().SetCause(1)).PrintStackTrace() }