Compare commits

..

No commits in common. "13e53f0c88083b9d9b32ae6a458c751a00ff768b" and "875957f6627c8283d7e4f7d5bb136c4be1f08978" have entirely different histories.

2 changed files with 29 additions and 27 deletions

View File

@ -8,27 +8,26 @@ import (
)
type Error struct {
Code int64
Msg string
StackTrace string
msg string
code int64
collection string
index string
stackTrace string
err error // Wrapped error
}
func NewErr(code int64, msg string) *Error {
return &Error{
Code: code,
Msg: msg,
msg: msg,
code: code,
}
}
func (e *Error) Error() string {
if e.collection != "" || e.index != "" {
return fmt.Sprintf(`[%d] (%s/%s) %s`, e.Code, e.collection, e.index, e.Msg)
return fmt.Sprintf(`[%d] (%s/%s) %s`, e.code, e.collection, e.index, e.msg)
} else {
return fmt.Sprintf("[%d] %s", e.Code, e.Msg)
return fmt.Sprintf("[%d] %s", e.code, e.msg)
}
}
@ -37,15 +36,11 @@ func (e *Error) Is(rhs error) bool {
if !ok {
return false
}
return e.Code == e2.Code
}
func (e *Error) Unwrap() error {
return e.err
return e.code == e2.code
}
func (e *Error) WithErr(err error) *Error {
if e2, ok := err.(*Error); ok && e2.Code == e.Code {
if e2, ok := err.(*Error); ok && e2.code == e.code {
return e2
}
@ -54,11 +49,18 @@ func (e *Error) WithErr(err error) *Error {
return e2
}
func (e *Error) Unwrap() error {
if e.err != nil {
return e.err
}
return e
}
func (e *Error) WithMsg(msg string, args ...any) *Error {
err := *e
err.Msg += ": " + fmt.Sprintf(msg, args...)
if len(err.StackTrace) == 0 {
err.StackTrace = string(debug.Stack())
err.msg += ": " + fmt.Sprintf(msg, args...)
if len(err.stackTrace) == 0 {
err.stackTrace = string(debug.Stack())
}
return &err
}
@ -76,16 +78,16 @@ func (e *Error) WithIndex(s string) *Error {
}
func (e *Error) msgTruncacted() string {
if len(e.Msg) > 255 {
return e.Msg[:255]
if len(e.msg) > 255 {
return e.msg[:255]
}
return e.Msg
return e.msg
}
func (e *Error) Write(w io.Writer) error {
msg := e.msgTruncacted()
if err := binary.Write(w, binary.LittleEndian, e.Code); err != nil {
if err := binary.Write(w, binary.LittleEndian, e.code); err != nil {
return IO.WithErr(err)
}
@ -101,7 +103,7 @@ func (e *Error) Read(r io.Reader) error {
size uint8
)
if err := binary.Read(r, binary.LittleEndian, &e.Code); err != nil {
if err := binary.Read(r, binary.LittleEndian, &e.code); err != nil {
return IO.WithErr(err)
}
@ -114,6 +116,6 @@ func (e *Error) Read(r io.Reader) error {
return IO.WithErr(err)
}
e.Msg = string(msgBuf)
e.msg = string(msgBuf)
return nil
}

View File

@ -10,12 +10,12 @@ func FmtDetails(err error) string {
var s string
if e.collection != "" || e.index != "" {
s = fmt.Sprintf(`[%d] (%s/%s) %s`, e.Code, e.collection, e.index, e.Msg)
s = fmt.Sprintf(`[%d] (%s/%s) %s`, e.code, e.collection, e.index, e.msg)
} else {
s = fmt.Sprintf("[%d] %s", e.Code, e.Msg)
s = fmt.Sprintf("[%d] %s", e.code, e.msg)
}
if len(e.StackTrace) != 0 {
s += "\n\nStack Trace:\n" + e.StackTrace + "\n"
if len(e.stackTrace) != 0 {
s += "\n\nStack Trace:\n" + e.stackTrace + "\n"
}
return s