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

View File

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