This commit is contained in:
tursom 2022-03-31 01:22:30 +08:00
parent cce87f820e
commit 9602d80b4e
13 changed files with 81 additions and 154 deletions

View File

@ -1,5 +1,5 @@
package exceptions package exceptions
var ElementFound = NewRuntimeException("", "element found", DefaultExceptionConfig().SetGetStackTrace(false)) var ElementFound = NewRuntimeException("", DefaultExceptionConfig().SetGetStackTrace(false).SetExceptionName("ElementFound"))
var ElementNotFound = NewRuntimeException("", "element not found", DefaultExceptionConfig().SetGetStackTrace(false)) var ElementNotFound = NewRuntimeException("", DefaultExceptionConfig().SetGetStackTrace(false).SetExceptionName("ElementNotFound"))
var CollectionLoopFinished = NewRuntimeException("", "collection loop finished", DefaultExceptionConfig().SetGetStackTrace(false)) var CollectionLoopFinished = NewRuntimeException("", DefaultExceptionConfig().SetGetStackTrace(false).SetExceptionName("CollectionLoopFinished"))

View File

@ -4,12 +4,8 @@ type ElementNotFoundException struct {
RuntimeException RuntimeException
} }
func NewElementNotFoundException(message any, config *ExceptionConfig) *ElementNotFoundException { func NewElementNotFoundException(message string, config *ExceptionConfig) *ElementNotFoundException {
return &ElementNotFoundException{ return &ElementNotFoundException{
NewRuntimeException( NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("ElementNotFoundException")),
message,
"exception caused ElementNotFoundException:",
config.AddSkipStack(1),
),
} }
} }

View File

@ -10,7 +10,8 @@ import (
type Exception interface { type Exception interface {
Cause() Exception Cause() Exception
Error() string Error() string
ErrorMessage() string Name() string
Message() string
StackTrace() []StackTrace StackTrace() []StackTrace
PrintStackTrace() PrintStackTrace()
PrintStackTraceTo(writer io.Writer) PrintStackTraceTo(writer io.Writer)
@ -46,8 +47,8 @@ func BuildStackTraceByArray(builder *strings.Builder, trace []StackTrace) {
} }
} }
func BuildStackTrace(builder *strings.Builder, e Exception, exceptionMsg string) { func BuildStackTrace(builder *strings.Builder, e Exception) {
builder.WriteString(fmt.Sprintln(exceptionMsg, e.ErrorMessage())) builder.WriteString(fmt.Sprintf("exception caused %s: %s\n", e.Name(), e.Message()))
if e.StackTrace() == nil { if e.StackTrace() == nil {
return return
} }
@ -93,7 +94,7 @@ func Package(err error) Exception {
case Exception: case Exception:
return err.(Exception) return err.(Exception)
} }
return NewRuntimeException(err, "", DefaultExceptionConfig().SetCause(err)) return NewRuntimeException("", DefaultExceptionConfig().SetCause(err))
} }
func PackageAny(err any) Exception { func PackageAny(err any) Exception {
@ -104,7 +105,7 @@ func PackageAny(err any) Exception {
case error: case error:
return Package(err.(error)) return Package(err.(error))
default: default:
return NewRuntimeException(err, "", DefaultExceptionConfig()) return NewRuntimeException("", DefaultExceptionConfig())
} }
} }
@ -114,16 +115,8 @@ func PackagePanic(panic any, exceptionMessage string) Exception {
} }
switch panic.(type) { switch panic.(type) {
case error: case error:
return NewRuntimeException( return NewRuntimeException("", DefaultExceptionConfig().SetCause(panic))
panic,
exceptionMessage,
DefaultExceptionConfig().SetCause(panic),
)
default: default:
return NewRuntimeException( return NewRuntimeException("", DefaultExceptionConfig())
panic,
exceptionMessage,
DefaultExceptionConfig(),
)
} }
} }

View File

@ -4,6 +4,7 @@ type ExceptionConfig struct {
SkipStack int SkipStack int
GetStackTrace bool GetStackTrace bool
Cause any Cause any
ExceptionName string
} }
func DefaultExceptionConfig() *ExceptionConfig { func DefaultExceptionConfig() *ExceptionConfig {
@ -16,7 +17,7 @@ func DefaultExceptionConfig() *ExceptionConfig {
func (c *ExceptionConfig) SetSkipStack(skipStack int) *ExceptionConfig { func (c *ExceptionConfig) SetSkipStack(skipStack int) *ExceptionConfig {
if c == nil { if c == nil {
return DefaultExceptionConfig().SetSkipStack(skipStack) return &ExceptionConfig{SkipStack: skipStack}
} }
c.SkipStack = skipStack c.SkipStack = skipStack
return c return c
@ -24,7 +25,7 @@ func (c *ExceptionConfig) SetSkipStack(skipStack int) *ExceptionConfig {
func (c *ExceptionConfig) SetGetStackTrace(getStackTrace bool) *ExceptionConfig { func (c *ExceptionConfig) SetGetStackTrace(getStackTrace bool) *ExceptionConfig {
if c == nil { if c == nil {
return DefaultExceptionConfig().SetGetStackTrace(getStackTrace) return &ExceptionConfig{GetStackTrace: getStackTrace}
} }
c.GetStackTrace = getStackTrace c.GetStackTrace = getStackTrace
return c return c
@ -32,7 +33,7 @@ func (c *ExceptionConfig) SetGetStackTrace(getStackTrace bool) *ExceptionConfig
func (c *ExceptionConfig) SetCause(cause any) *ExceptionConfig { func (c *ExceptionConfig) SetCause(cause any) *ExceptionConfig {
if c == nil { if c == nil {
return DefaultExceptionConfig().SetCause(cause) return &ExceptionConfig{Cause: cause}
} }
c.Cause = cause c.Cause = cause
return c return c
@ -40,8 +41,16 @@ func (c *ExceptionConfig) SetCause(cause any) *ExceptionConfig {
func (c *ExceptionConfig) AddSkipStack(skipStack int) *ExceptionConfig { func (c *ExceptionConfig) AddSkipStack(skipStack int) *ExceptionConfig {
if c == nil { if c == nil {
return DefaultExceptionConfig().AddSkipStack(skipStack) return &ExceptionConfig{SkipStack: skipStack}
} }
c.SkipStack += skipStack c.SkipStack += skipStack
return c return c
} }
func (c *ExceptionConfig) SetExceptionName(exceptionName string) *ExceptionConfig {
if c == nil {
return &ExceptionConfig{ExceptionName: exceptionName}
}
c.ExceptionName = exceptionName
return c
}

View File

@ -4,12 +4,8 @@ type IllegalParameterException struct {
RuntimeException RuntimeException
} }
func NewIllegalParameterException(message any, config *ExceptionConfig) *IllegalParameterException { func NewIllegalParameterException(message string, config *ExceptionConfig) *IllegalParameterException {
return &IllegalParameterException{ return &IllegalParameterException{
NewRuntimeException( NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("IllegalParameterException")),
message,
"exception caused ElementNotFoundException:",
config.AddSkipStack(1),
),
} }
} }

View File

@ -4,13 +4,9 @@ type IndexOutOfBound struct {
RuntimeException RuntimeException
} }
func NewIndexOutOfBound(message any, config *ExceptionConfig) *IndexOutOfBound { func NewIndexOutOfBound(message string, config *ExceptionConfig) *IndexOutOfBound {
return &IndexOutOfBound{ return &IndexOutOfBound{
NewRuntimeException( NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("IndexOutOfBound")),
message,
"exception caused IndexOutOfBound:",
config.AddSkipStack(1),
),
} }
} }
@ -18,7 +14,7 @@ func CatchIndexOutOfBound[T any](f func() T, config *ExceptionConfig) (r T, err
defer func() { defer func() {
r := recover() r := recover()
if r != nil { if r != nil {
err = NewIndexOutOfBound(r, config.AddSkipStack(3)) err = NewIndexOutOfBound("", config.AddSkipStack(3).SetCause(PackageAny(r)))
} }
}() }()
r = f() r = f()

View File

@ -9,13 +9,9 @@ type NPE struct {
RuntimeException RuntimeException
} }
func NewNPE(message any, config *ExceptionConfig) *NPE { func NewNPE(message string, config *ExceptionConfig) *NPE {
return &NPE{ return &NPE{
NewRuntimeException( NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("NPE")),
message,
"exception caused NullPointerException:",
config.AddSkipStack(1),
),
} }
} }

View File

@ -4,12 +4,8 @@ type OperationNotSupportedException struct {
RuntimeException RuntimeException
} }
func NewOperationNotSupportedException(message any, config *ExceptionConfig) *OperationNotSupportedException { func NewOperationNotSupportedException(message string, config *ExceptionConfig) *OperationNotSupportedException {
return &OperationNotSupportedException{ return &OperationNotSupportedException{
NewRuntimeException( NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("OperationNotSupportedException")),
message,
"exception caused OperationNotSupportedException:",
config.AddSkipStack(1),
),
} }
} }

View File

@ -1,13 +1,27 @@
package exceptions package exceptions
import (
"fmt"
"reflect"
)
type PackageException struct { type PackageException struct {
RuntimeException RuntimeException
err any 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{ return &PackageException{
RuntimeException: NewRuntimeException(err, exceptionMessage, config.AddSkipStack(1)), RuntimeException: NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("PackageException")),
err: err, err: err,
} }
} }

View File

@ -1,7 +1,6 @@
package exceptions package exceptions
import ( import (
"fmt"
"github.com/tursom/GoCollections/lang" "github.com/tursom/GoCollections/lang"
"io" "io"
"os" "os"
@ -10,13 +9,13 @@ import (
type RuntimeException struct { type RuntimeException struct {
lang.BaseObject lang.BaseObject
message string message string
exceptionMessage string exceptionName string
stackTrace []StackTrace stackTrace []StackTrace
cause Exception cause Exception
} }
func NewRuntimeException(message any, exceptionMessage string, config *ExceptionConfig) RuntimeException { func NewRuntimeException(message string, config *ExceptionConfig) RuntimeException {
if config == nil { if config == nil {
config = DefaultExceptionConfig() config = DefaultExceptionConfig()
} }
@ -26,27 +25,28 @@ func NewRuntimeException(message any, exceptionMessage string, config *Exception
stackTrace = GetStackTraceSkipDeep(config.SkipStack + 1) stackTrace = GetStackTraceSkipDeep(config.SkipStack + 1)
} }
if len(exceptionMessage) == 0 {
exceptionMessage = "exception caused:"
}
var causeException Exception = nil var causeException Exception = nil
if config.Cause != nil { if config.Cause != nil {
switch config.Cause.(type) { switch e := config.Cause.(type) {
case Exception: case Exception:
causeException = config.Cause.(Exception) causeException = e
default: default:
causeException = NewPackageException(config.Cause, "exception caused:", DefaultExceptionConfig(). causeException = NewPackageException(config.Cause, DefaultExceptionConfig().
SetGetStackTrace(false)) SetGetStackTrace(false))
} }
} }
exceptionName := "RuntimeException"
if len(config.ExceptionName) != 0 {
exceptionName = config.ExceptionName
}
return RuntimeException{ return RuntimeException{
BaseObject: lang.NewBaseObject(), BaseObject: lang.NewBaseObject(),
message: fmt.Sprint(message), message: message,
exceptionMessage: exceptionMessage, stackTrace: stackTrace,
stackTrace: stackTrace, cause: causeException,
cause: causeException, exceptionName: exceptionName,
} }
} }
@ -60,8 +60,12 @@ func (o RuntimeException) Error() string {
return builder.String() return builder.String()
} }
func (o RuntimeException) ErrorMessage() string { func (o RuntimeException) Message() string {
return o.exceptionMessage return o.message
}
func (o RuntimeException) Name() string {
return o.exceptionName
} }
func (o RuntimeException) StackTrace() []StackTrace { func (o RuntimeException) StackTrace() []StackTrace {
@ -86,7 +90,7 @@ func (o RuntimeException) PrintStackTraceTo(writer io.Writer) {
} }
func (o RuntimeException) BuildPrintStackTrace(builder *strings.Builder) { func (o RuntimeException) BuildPrintStackTrace(builder *strings.Builder) {
BuildStackTrace(builder, o, o.exceptionMessage) BuildStackTrace(builder, o)
if o.cause != nil { if o.cause != nil {
builder.WriteString("caused by: ") builder.WriteString("caused by: ")
o.cause.BuildPrintStackTrace(builder) o.cause.BuildPrintStackTrace(builder)

View File

@ -10,13 +10,9 @@ type TypeCastException struct {
RuntimeException RuntimeException
} }
func NewTypeCastException(message any, config *ExceptionConfig) *TypeCastException { func NewTypeCastException(message string, config *ExceptionConfig) *TypeCastException {
return &TypeCastException{ return &TypeCastException{
NewRuntimeException( NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("TypeCastException")),
message,
"exception caused ElementNotFoundException:",
config.AddSkipStack(1),
),
} }
} }

View File

@ -6,6 +6,6 @@ type WrongCallHostException struct {
func NewWrongCallHostException(message string) WrongCallHostException { func NewWrongCallHostException(message string) WrongCallHostException {
return WrongCallHostException{ return WrongCallHostException{
NewRuntimeException(nil, message, DefaultExceptionConfig().AddSkipStack(1)), NewRuntimeException(message, DefaultExceptionConfig().AddSkipStack(1).SetExceptionName("WrongCallHostException")),
} }
} }

73
main.go
View File

@ -1,76 +1,7 @@
package main package main
import ( import "github.com/tursom/GoCollections/exceptions"
"fmt"
"github.com/tursom/GoCollections/collections"
"github.com/tursom/GoCollections/exceptions"
"github.com/tursom/GoCollections/lang"
"time"
)
func main() { func main() {
_, err := exceptions.Try(func() (any, exceptions.Exception) { exceptions.NewRuntimeException("test2", exceptions.DefaultExceptionConfig().SetCause(1)).PrintStackTrace()
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)
//}
} }