/
n0dwis
/
errors
Обзор
Документация
Войти
/
n0dwis
/
errors
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
rich_error/error.go
103 строки
2 KB
Dmitry Vakulenko
Small refafctoring
15 фев 2026, 17:20
15 фев 2026, 17:20
e03812f
Код
Авторство
О чём код?
package rich_error import ( stdErr "errors" "fmt" "log/slog" "runtime" ) type Error struct { Kind fmt.Stringer Code fmt.Stringer Message string Meta []slog.Attr Stacktrace []uintptr Previous error } func Is(err, target error) bool { return stdErr.Is(err, target) } func As(err error, target any) bool { return stdErr.As(err, target) } func WrapWithStack(err error, kind, code fmt.Stringer, message string, attrs ...slog.Attr) *Error { res := &Error{ Kind: kind, Code: code, Message: message, Stacktrace: buildStack(), Meta: attrs, Previous: err, } return res } func Wrap(err error, kind, code fmt.Stringer, message string, attrs ...slog.Attr) *Error { res := &Error{ Kind: kind, Code: code, Message: message, Meta: attrs, Previous: err, } return res } func WrapMeta(err error, attrs ...slog.Attr) *Error { res := &Error{ Meta: attrs, Previous: err, } return res } func WrapMetaWithStack(err error, attrs ...slog.Attr) *Error { res := &Error{ Stacktrace: buildStack(), Meta: attrs, Previous: err, } return res } func New(kind, code fmt.Stringer, message string, attrs ...slog.Attr) *Error { res := &Error{ Kind: kind, Code: code, Message: message, Stacktrace: buildStack(), Meta: attrs, } return res } func buildStack() []uintptr { stackFrames := make([]uintptr, 32) n := runtime.Callers(3, stackFrames) return stackFrames[:n] } func (e *Error) Error() string { return e.Message } func (e *Error) Unwrap() error { return e.Previous } func (e *Error) LogAttrs() []slog.Attr { return e.Meta } func (e *Error) Stack() []uintptr { return e.Stacktrace }