mirror of
https://github.com/tursom/GoCollections.git
synced 2025-03-13 17:00:18 +08:00
update
This commit is contained in:
parent
cce87f820e
commit
9602d80b4e
@ -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"))
|
||||
|
@ -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")),
|
||||
}
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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")),
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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")),
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package exceptions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/tursom/GoCollections/lang"
|
||||
"io"
|
||||
"os"
|
||||
@ -11,12 +10,12 @@ import (
|
||||
type RuntimeException struct {
|
||||
lang.BaseObject
|
||||
message string
|
||||
exceptionMessage 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,
|
||||
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)
|
||||
|
@ -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")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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")),
|
||||
}
|
||||
}
|
||||
|
73
main.go
73
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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user