Initial commit
This commit is contained in:
		
							
								
								
									
										121
									
								
								lib/errs/error.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										121
									
								
								lib/errs/error.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,121 @@ | ||||
| package errs | ||||
|  | ||||
| import ( | ||||
| 	"encoding/binary" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"runtime/debug" | ||||
| ) | ||||
|  | ||||
| type Error struct { | ||||
| 	msg        string | ||||
| 	code       int64 | ||||
| 	collection string | ||||
| 	index      string | ||||
| 	stackTrace string | ||||
| 	err        error // Wrapped error | ||||
| } | ||||
|  | ||||
| func NewErr(code int64, msg string) *Error { | ||||
| 	return &Error{ | ||||
| 		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) | ||||
| 	} else { | ||||
| 		return fmt.Sprintf("[%d] %s", e.code, e.msg) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (e *Error) Is(rhs error) bool { | ||||
| 	e2, ok := rhs.(*Error) | ||||
| 	if !ok { | ||||
| 		return false | ||||
| 	} | ||||
| 	return e.code == e2.code | ||||
| } | ||||
|  | ||||
| func (e *Error) WithErr(err error) *Error { | ||||
| 	if e2, ok := err.(*Error); ok && e2.code == e.code { | ||||
| 		return e2 | ||||
| 	} | ||||
|  | ||||
| 	e2 := e.WithMsg(err.Error()) | ||||
| 	e2.err = err | ||||
| 	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()) | ||||
| 	} | ||||
| 	return &err | ||||
| } | ||||
|  | ||||
| func (e *Error) WithCollection(s string) *Error { | ||||
| 	err := *e | ||||
| 	err.collection = s | ||||
| 	return &err | ||||
| } | ||||
|  | ||||
| func (e *Error) WithIndex(s string) *Error { | ||||
| 	err := *e | ||||
| 	err.index = s | ||||
| 	return &err | ||||
| } | ||||
|  | ||||
| func (e *Error) msgTruncacted() string { | ||||
| 	if len(e.msg) > 255 { | ||||
| 		return e.msg[:255] | ||||
| 	} | ||||
| 	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 { | ||||
| 		return IO.WithErr(err) | ||||
| 	} | ||||
|  | ||||
| 	if _, err := w.Write([]byte{byte(len(msg))}); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	_, err := w.Write([]byte(msg)) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (e *Error) Read(r io.Reader) error { | ||||
| 	var ( | ||||
| 		size uint8 | ||||
| 	) | ||||
|  | ||||
| 	if err := binary.Read(r, binary.LittleEndian, &e.code); err != nil { | ||||
| 		return IO.WithErr(err) | ||||
| 	} | ||||
|  | ||||
| 	if err := binary.Read(r, binary.LittleEndian, &size); err != nil { | ||||
| 		return IO.WithErr(err) | ||||
| 	} | ||||
|  | ||||
| 	msgBuf := make([]byte, size) | ||||
| 	if _, err := io.ReadFull(r, msgBuf); err != nil { | ||||
| 		return IO.WithErr(err) | ||||
| 	} | ||||
|  | ||||
| 	e.msg = string(msgBuf) | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										26
									
								
								lib/errs/error_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								lib/errs/error_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| package errs | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestError_Simple(t *testing.T) { | ||||
| 	e := Archived | ||||
|  | ||||
| 	b := &bytes.Buffer{} | ||||
|  | ||||
| 	if err := e.Write(b); err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	e2 := &Error{} | ||||
| 	if err := e2.Read(b); err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	if !reflect.DeepEqual(*e, *e2) { | ||||
| 		t.Fatal("not equal") | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										21
									
								
								lib/errs/errors.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								lib/errs/errors.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| package errs | ||||
|  | ||||
| var ( | ||||
| 	Archived      = NewErr(100, "archived") | ||||
| 	EOFArchived   = NewErr(101, "EOF-archived") | ||||
| 	IO            = NewErr(102, "IO error") | ||||
| 	NotFound      = NewErr(103, "not found") | ||||
| 	Locked        = NewErr(104, "locked") | ||||
| 	NotAuthorized = NewErr(105, "not authorized") | ||||
| 	NotAllowed    = NewErr(106, "not allowed") | ||||
| 	Stopped       = NewErr(107, "stopped") | ||||
| 	Timeout       = NewErr(108, "timeout") | ||||
| 	Duplicate     = NewErr(109, "duplicate") | ||||
| 	ReadOnly      = NewErr(110, "read only") | ||||
| 	Encoding      = NewErr(111, "encoding") | ||||
| 	Closed        = NewErr(112, "closed") | ||||
| 	InvalidPath   = NewErr(200, "invalid path") | ||||
| 	Corrupt       = NewErr(666, "corrupt") | ||||
| 	Fatal         = NewErr(1053, "fatal") | ||||
| 	Unexpected    = NewErr(999, "unexpected") | ||||
| ) | ||||
							
								
								
									
										22
									
								
								lib/errs/fmt.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								lib/errs/fmt.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| package errs | ||||
|  | ||||
| import "fmt" | ||||
|  | ||||
| func FmtDetails(err error) string { | ||||
| 	e, ok := err.(*Error) | ||||
| 	if !ok { | ||||
| 		return err.Error() | ||||
| 	} | ||||
|  | ||||
| 	var s string | ||||
| 	if e.collection != "" || e.index != "" { | ||||
| 		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) | ||||
| 	} | ||||
| 	if len(e.stackTrace) != 0 { | ||||
| 		s += "\n\nStack Trace:\n" + e.stackTrace + "\n" | ||||
| 	} | ||||
|  | ||||
| 	return s | ||||
| } | ||||
		Reference in New Issue
	
	Block a user