GoCollections/exceptions/RuntimeException.go

99 lines
2.0 KiB
Go
Raw Normal View History

2021-05-21 10:31:49 +08:00
package exceptions
import (
2022-03-23 10:15:18 +08:00
"github.com/tursom/GoCollections/lang"
2021-05-21 10:31:49 +08:00
"io"
"os"
2021-05-21 11:14:07 +08:00
"strings"
2021-05-21 10:31:49 +08:00
)
type RuntimeException struct {
2022-03-23 10:15:18 +08:00
lang.BaseObject
2022-03-31 01:22:30 +08:00
message string
exceptionName string
stackTrace []StackTrace
cause Exception
2021-05-21 10:31:49 +08:00
}
2022-03-31 01:22:30 +08:00
func NewRuntimeException(message string, config *ExceptionConfig) RuntimeException {
2022-03-21 11:02:41 +08:00
if config == nil {
config = DefaultExceptionConfig()
}
2021-05-21 10:31:49 +08:00
var stackTrace []StackTrace = nil
2022-03-21 11:02:41 +08:00
if config.GetStackTrace {
stackTrace = GetStackTraceSkipDeep(config.SkipStack + 1)
2021-05-21 10:31:49 +08:00
}
2021-05-21 11:18:56 +08:00
var causeException Exception = nil
2022-03-21 11:02:41 +08:00
if config.Cause != nil {
2022-03-31 01:22:30 +08:00
switch e := config.Cause.(type) {
2021-05-21 11:25:53 +08:00
case Exception:
2022-03-31 01:22:30 +08:00
causeException = e
2021-05-21 11:25:53 +08:00
default:
2022-03-31 01:22:30 +08:00
causeException = NewPackageException(config.Cause, DefaultExceptionConfig().
2022-03-21 11:02:41 +08:00
SetGetStackTrace(false))
2021-05-21 11:25:53 +08:00
}
2021-05-21 11:18:56 +08:00
}
2022-03-31 01:22:30 +08:00
exceptionName := "RuntimeException"
if len(config.ExceptionName) != 0 {
exceptionName = config.ExceptionName
}
2021-05-21 10:31:49 +08:00
return RuntimeException{
2022-03-31 01:22:30 +08:00
BaseObject: lang.NewBaseObject(),
message: message,
stackTrace: stackTrace,
cause: causeException,
exceptionName: exceptionName,
2021-05-21 10:31:49 +08:00
}
}
2021-05-21 11:14:07 +08:00
func (o RuntimeException) Cause() Exception {
return o.cause
}
2021-05-21 10:31:49 +08:00
func (o RuntimeException) Error() string {
2022-03-28 18:19:05 +08:00
builder := strings.Builder{}
o.BuildPrintStackTrace(&builder)
return builder.String()
2021-05-21 10:31:49 +08:00
}
2022-03-31 01:22:30 +08:00
func (o RuntimeException) Message() string {
return o.message
}
func (o RuntimeException) Name() string {
return o.exceptionName
}
2021-05-21 10:31:49 +08:00
func (o RuntimeException) StackTrace() []StackTrace {
return o.stackTrace
}
func (o RuntimeException) PrintStackTrace() {
o.PrintStackTraceTo(os.Stderr)
}
func (o RuntimeException) PrintStackTraceTo(writer io.Writer) {
2022-03-28 18:19:05 +08:00
bytes := []byte(o.Error())
2021-05-21 11:14:07 +08:00
writeBytes := 0
for writeBytes < len(bytes) {
write, err := writer.Write(bytes[writeBytes:])
if err != nil {
2021-05-21 13:31:41 +08:00
Print(err)
2021-05-21 11:14:07 +08:00
return
}
writeBytes += write
}
}
func (o RuntimeException) BuildPrintStackTrace(builder *strings.Builder) {
2022-03-31 01:22:30 +08:00
BuildStackTrace(builder, o)
2021-05-21 11:14:07 +08:00
if o.cause != nil {
builder.WriteString("caused by: ")
o.cause.BuildPrintStackTrace(builder)
}
2021-05-21 10:31:49 +08:00
}