move important exceptions to package lang

This commit is contained in:
tursom 2022-11-26 14:05:48 +08:00
parent 8725d7b4a5
commit acd5df2410
12 changed files with 376 additions and 268 deletions

View File

@ -15,16 +15,7 @@ import (
"github.com/tursom/GoCollections/lang"
)
type Exception interface {
Cause() Exception
Error() string
Name() string
Message() string
StackTrace() []lang.StackTrace
PrintStackTrace()
PrintStackTraceTo(writer io.Writer)
BuildPrintStackTrace(builder *strings.Builder)
}
type Exception = lang.Exception
func PrintStackTraceByArray(writer io.Writer, trace []lang.StackTrace) {
if trace == nil {

View File

@ -6,61 +6,14 @@
package exceptions
type ExceptionConfig struct {
SkipStack int
GetStackTrace bool
Cause any
ExceptionName string
}
import "github.com/tursom/GoCollections/lang"
type ExceptionConfig = lang.ExceptionConfig
func Cfg() *ExceptionConfig {
return DefaultExceptionConfig()
}
func DefaultExceptionConfig() *ExceptionConfig {
return &ExceptionConfig{
SkipStack: 0,
GetStackTrace: true,
Cause: nil,
}
}
func (c *ExceptionConfig) SetSkipStack(skipStack int) *ExceptionConfig {
if c == nil {
return &ExceptionConfig{SkipStack: skipStack}
}
c.SkipStack = skipStack
return c
}
func (c *ExceptionConfig) SetGetStackTrace(getStackTrace bool) *ExceptionConfig {
if c == nil {
return &ExceptionConfig{GetStackTrace: getStackTrace}
}
c.GetStackTrace = getStackTrace
return c
}
func (c *ExceptionConfig) SetCause(cause any) *ExceptionConfig {
if c == nil {
return &ExceptionConfig{Cause: cause}
}
c.Cause = cause
return c
}
func (c *ExceptionConfig) AddSkipStack(skipStack int) *ExceptionConfig {
if c == nil {
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
return lang.DefaultExceptionConfig()
}

View File

@ -18,7 +18,7 @@ type NPE struct {
func NewNPE(message string, config *ExceptionConfig) *NPE {
return &NPE{
NewRuntimeException(message, config.AddSkipStack(1).
*NewRuntimeException(message, config.AddSkipStack(1).
SetExceptionName("github.com.tursom.GoCollections.exceptions.NPE")),
}
}

View File

@ -7,49 +7,14 @@
package exceptions
import (
"fmt"
"reflect"
"github.com/tursom/GoCollections/lang"
)
type PackageException struct {
RuntimeException
err any
}
type PackageException = lang.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(message, config.AddSkipStack(1).
SetExceptionName("github.com.tursom.GoCollections.exceptions.PackageException")),
err: err,
}
return lang.NewPackageException(err, config.AddSkipStack(1))
}
func (p *PackageException) Err() any {
return p.err
}
func UnpackException(err any) any {
for err != nil {
switch e := err.(type) {
case *PackageException:
return e.Err()
case Exception:
err = e.Cause()
if err == nil {
return e
}
default:
return err
}
}
return nil
return lang.UnpackException(err)
}

View File

@ -7,110 +7,11 @@
package exceptions
import (
"fmt"
"io"
"os"
"strings"
"github.com/tursom/GoCollections/lang"
)
type RuntimeException struct {
lang.BaseObject
message string
exceptionName string
stackTrace []lang.StackTrace
cause Exception
}
func NewRuntimeException(message string, config *ExceptionConfig) RuntimeException {
if config == nil {
config = DefaultExceptionConfig()
}
var stackTrace []lang.StackTrace = nil
if config.GetStackTrace {
stackTrace = lang.GetStackTraceSkipDeep(config.SkipStack + 1)
}
var causeException Exception = nil
if config.Cause != nil {
switch e := config.Cause.(type) {
case Exception:
causeException = e
default:
causeException = NewPackageException(config.Cause, DefaultExceptionConfig().
SetGetStackTrace(false))
}
}
exceptionName := "github.com.tursom.GoCollections.exceptions.RuntimeException"
if len(config.ExceptionName) != 0 {
exceptionName = config.ExceptionName
}
return RuntimeException{
BaseObject: lang.NewBaseObject(),
message: message,
stackTrace: stackTrace,
cause: causeException,
exceptionName: exceptionName,
}
}
func (o RuntimeException) Cause() Exception {
return o.cause
}
func (o RuntimeException) Error() string {
message := o.message
if len(message) == 0 {
if o.cause != nil {
message = fmt.Sprintf("%s: %s", o.Name(), o.cause.Error())
} else {
message = o.Name()
}
} else {
message = fmt.Sprintf("%s: %s", o.Name(), message)
}
return message
}
func (o RuntimeException) Message() string {
return o.message
}
func (o RuntimeException) Name() string {
return o.exceptionName
}
func (o RuntimeException) StackTrace() []lang.StackTrace {
return o.stackTrace
}
func (o RuntimeException) PrintStackTrace() {
o.PrintStackTraceTo(os.Stderr)
}
func (o RuntimeException) PrintStackTraceTo(writer io.Writer) {
builder := strings.Builder{}
o.BuildPrintStackTrace(&builder)
bytes := []byte(builder.String())
writeBytes := 0
for writeBytes < len(bytes) {
write, err := writer.Write(bytes[writeBytes:])
if err != nil {
Print(err)
return
}
writeBytes += write
}
}
func (o RuntimeException) BuildPrintStackTrace(builder *strings.Builder) {
BuildStackTrace(builder, o)
if o.cause != nil {
builder.WriteString("caused by: ")
o.cause.BuildPrintStackTrace(builder)
}
type RuntimeException = lang.RuntimeException
func NewRuntimeException(message string, config *ExceptionConfig) *RuntimeException {
return lang.NewRuntimeException(message, config.AddSkipStack(1))
}

View File

@ -13,13 +13,11 @@ import (
"github.com/tursom/GoCollections/lang"
)
type TypeCastException struct {
RuntimeException
}
type TypeCastException = lang.TypeCastException
func NewTypeCastException(message string, config *ExceptionConfig) *TypeCastException {
return &TypeCastException{
NewRuntimeException(message, config.AddSkipStack(1).
*NewRuntimeException(message, config.AddSkipStack(1).
SetExceptionName("github.com.tursom.GoCollections.exceptions.TypeCastException")),
}
}

100
lang/Exception.go Normal file
View File

@ -0,0 +1,100 @@
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
package lang
import (
"fmt"
"io"
"os"
"strings"
)
type Exception interface {
Cause() Exception
Error() string
Name() string
Message() string
StackTrace() []StackTrace
PrintStackTrace()
PrintStackTraceTo(writer io.Writer)
BuildPrintStackTrace(builder *strings.Builder)
}
func PrintStackTraceByArray(writer io.Writer, trace []StackTrace) {
if trace == nil {
return
}
builder := &strings.Builder{}
for _, stackTrace := range trace {
stackTrace.WriteTo(builder)
}
bytes := []byte(builder.String())
writeBytes := 0
for writeBytes < len(bytes) {
write, err := writer.Write(bytes[writeBytes:])
if err != nil {
Print(err)
return
}
writeBytes += write
}
}
func BuildStackTraceByArray(builder *strings.Builder, trace []StackTrace) {
if trace == nil {
return
}
for _, stackTrace := range trace {
stackTrace.WriteTo(builder)
}
}
func BuildStackTrace(builder *strings.Builder, e Exception) {
builder.WriteString(e.Error())
builder.WriteString("\n")
if e.StackTrace() == nil {
return
}
for _, stackTrace := range e.StackTrace() {
stackTrace.WriteTo(builder)
}
}
func GetStackTraceString(e Exception) string {
builder := &strings.Builder{}
BuildStackTrace(builder, e)
return builder.String()
}
func Try[R any](
f func() (ret R, err Exception),
catch func(panic any) (ret R, err Exception),
) (ret R, err Exception) {
defer func() {
if r := recover(); r != nil {
ret, err = catch(r)
}
}()
ret, err = f()
if err != nil {
ret, err = catch(err)
}
return
}
func Print(err error) {
if err == nil {
return
}
switch err.(type) {
case Exception:
err.(Exception).PrintStackTrace()
default:
_, _ = fmt.Fprintln(os.Stderr, err)
PrintStackTraceByArray(os.Stderr, GetStackTrace())
}
}

66
lang/ExceptionConfig.go Normal file
View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
package lang
type ExceptionConfig struct {
SkipStack int
GetStackTrace bool
Cause any
ExceptionName string
}
func Cfg() *ExceptionConfig {
return DefaultExceptionConfig()
}
func DefaultExceptionConfig() *ExceptionConfig {
return &ExceptionConfig{
SkipStack: 0,
GetStackTrace: true,
Cause: nil,
}
}
func (c *ExceptionConfig) SetSkipStack(skipStack int) *ExceptionConfig {
if c == nil {
return &ExceptionConfig{SkipStack: skipStack}
}
c.SkipStack = skipStack
return c
}
func (c *ExceptionConfig) SetGetStackTrace(getStackTrace bool) *ExceptionConfig {
if c == nil {
return &ExceptionConfig{GetStackTrace: getStackTrace}
}
c.GetStackTrace = getStackTrace
return c
}
func (c *ExceptionConfig) SetCause(cause any) *ExceptionConfig {
if c == nil {
return &ExceptionConfig{Cause: cause}
}
c.Cause = cause
return c
}
func (c *ExceptionConfig) AddSkipStack(skipStack int) *ExceptionConfig {
if c == nil {
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
}

View File

@ -7,20 +7,9 @@
package lang
import (
"fmt"
"io"
"os"
"strings"
"unsafe"
)
type TypeCastException struct {
BaseObject
message string
stackTrace []StackTrace
cause any
}
func Nil[T any]() T {
var n T
return n
@ -47,11 +36,7 @@ func Cast[T any](v any) T {
defer func() {
r := recover()
if r != nil {
panic(&TypeCastException{
message: "cast failed",
stackTrace: GetStackTraceSkipDeep(1),
cause: r,
})
panic(NewTypeCastException("", r, nil))
}
}()
@ -66,11 +51,7 @@ func ForceCast[T any](v unsafe.Pointer) *T {
defer func() {
r := recover()
if r != nil {
panic(&TypeCastException{
message: "cast failed",
stackTrace: GetStackTraceSkipDeep(1),
cause: r,
})
panic(NewTypeCastException("", r, nil))
}
}()
@ -80,42 +61,3 @@ func ForceCast[T any](v unsafe.Pointer) *T {
return (*T)(v)
}
}
func (t *TypeCastException) Error() string {
return fmt.Sprintf("TypeCastException: %s\ncause by: %s", t.message, t.cause)
}
func (t *TypeCastException) Name() string {
return "github.com.tursom.GoCollections.lang.TypeCastException"
}
func (t *TypeCastException) Message() string {
return t.message
}
func (t *TypeCastException) StackTrace() []StackTrace {
return t.stackTrace
}
func (t *TypeCastException) PrintStackTrace() {
t.PrintStackTraceTo(os.Stderr)
}
func (t *TypeCastException) PrintStackTraceTo(writer io.Writer) {
builder := strings.Builder{}
t.BuildPrintStackTrace(&builder)
bytes := []byte(builder.String())
writeBytes := 0
_, _ = writer.Write(bytes[writeBytes:])
}
func (t *TypeCastException) BuildPrintStackTrace(builder *strings.Builder) {
builder.WriteString(t.Error())
builder.WriteString("\n")
if t.StackTrace() == nil {
return
}
for _, stackTrace := range t.StackTrace() {
stackTrace.WriteTo(builder)
}
}

55
lang/PackageException.go Normal file
View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
package lang
import (
"fmt"
"reflect"
)
type PackageException struct {
RuntimeException
err any
}
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(message, config.AddSkipStack(1).
SetExceptionName("github.com.tursom.GoCollections.exceptions.PackageException")),
err: err,
}
}
func (p *PackageException) Err() any {
return p.err
}
func UnpackException(err any) any {
for err != nil {
switch e := err.(type) {
case *PackageException:
return e.Err()
case Exception:
err = e.Cause()
if err == nil {
return e
}
default:
return err
}
}
return nil
}

114
lang/RuntimeException.go Normal file
View File

@ -0,0 +1,114 @@
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
package lang
import (
"fmt"
"io"
"os"
"strings"
)
type RuntimeException struct {
BaseObject
message string
exceptionName string
stackTrace []StackTrace
cause Exception
}
func NewRuntimeException(message string, config *ExceptionConfig) *RuntimeException {
if config == nil {
config = DefaultExceptionConfig()
}
var stackTrace []StackTrace = nil
if config.GetStackTrace {
stackTrace = GetStackTraceSkipDeep(config.SkipStack + 1)
}
var causeException Exception = nil
if config.Cause != nil {
switch e := config.Cause.(type) {
case Exception:
causeException = e
default:
causeException = NewPackageException(config.Cause, DefaultExceptionConfig().
SetGetStackTrace(false))
}
}
exceptionName := "github.com.tursom.GoCollections.exceptions.RuntimeException"
if len(config.ExceptionName) != 0 {
exceptionName = config.ExceptionName
}
return &RuntimeException{
BaseObject: NewBaseObject(),
message: message,
stackTrace: stackTrace,
cause: causeException,
exceptionName: exceptionName,
}
}
func (o *RuntimeException) Cause() Exception {
return o.cause
}
func (o *RuntimeException) Error() string {
message := o.message
if len(message) == 0 {
if o.cause != nil {
message = fmt.Sprintf("%s: %s", o.Name(), o.cause.Error())
} else {
message = o.Name()
}
} else {
message = fmt.Sprintf("%s: %s", o.Name(), message)
}
return message
}
func (o *RuntimeException) Message() string {
return o.message
}
func (o *RuntimeException) Name() string {
return o.exceptionName
}
func (o *RuntimeException) StackTrace() []StackTrace {
return o.stackTrace
}
func (o *RuntimeException) PrintStackTrace() {
o.PrintStackTraceTo(os.Stderr)
}
func (o *RuntimeException) PrintStackTraceTo(writer io.Writer) {
builder := strings.Builder{}
o.BuildPrintStackTrace(&builder)
bytes := []byte(builder.String())
writeBytes := 0
for writeBytes < len(bytes) {
write, err := writer.Write(bytes[writeBytes:])
if err != nil {
Print(err)
return
}
writeBytes += write
}
}
func (o *RuntimeException) BuildPrintStackTrace(builder *strings.Builder) {
BuildStackTrace(builder, o)
if o.cause != nil {
builder.WriteString("caused by: ")
o.cause.BuildPrintStackTrace(builder)
}
}

23
lang/TypeCastException.go Normal file
View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
package lang
type TypeCastException struct {
RuntimeException
}
func NewTypeCastException(message string, err any, config *ExceptionConfig) *TypeCastException {
if message == "" {
message = "type cast failed"
}
return &TypeCastException{
RuntimeException: *NewRuntimeException(message, config.AddSkipStack(1).
SetCause(err).
SetExceptionName("github.com.tursom.GoCollections.exceptions.TypeCastException")),
}
}