errs: reexpose errors

This commit is contained in:
Tigor Hutasuhut 2024-08-15 21:44:17 +07:00
parent ab13359aeb
commit b472bb2ee8
2 changed files with 12 additions and 13 deletions

View file

@ -109,33 +109,32 @@ func (e *Err) Details(keysAndValues ...any) Error {
// discarding any empty and duplicate messages in the chain. // discarding any empty and duplicate messages in the chain.
// //
// The error messages are concatenated with a colon and a space. // The error messages are concatenated with a colon and a space.
//
// The message does not expose the origin error message.
//
// To get them, errors.Unwrap have to be called against this error
// or view them in the logs.
func (e *Err) Error() string { func (e *Err) Error() string {
s := strings.Builder{} s := strings.Builder{}
if e.message != "" { if e.message != "" {
s.WriteString(e.message) s.WriteString(e.message)
if e.origin != nil {
s.WriteString(": ")
}
} }
unwrap := errors.Unwrap(e) unwrap := errors.Unwrap(e)
for unwrap != nil { for unwrap != nil {
var current string var current string
if e, ok := unwrap.(Error); ok { if e, ok := unwrap.(Error); ok && e.GetMessage() != "" {
if msg := e.GetMessage(); msg != "" { current = e.GetMessage()
current = msg } else {
} current = unwrap.Error()
} }
next := errors.Unwrap(unwrap) next := errors.Unwrap(unwrap)
if next != nil { if next != nil {
current, _ = strings.CutSuffix(current, next.Error()) current, _ = strings.CutSuffix(current, next.Error())
current, _ = strings.CutSuffix(current, ": ")
} }
if current != "" { if current != "" {
if _, ok := next.(Error); ok { s.WriteString(current)
if next != nil {
s.WriteString(": ") s.WriteString(": ")
} }
s.WriteString(current)
} }
unwrap = next unwrap = next
@ -223,9 +222,9 @@ func Failf(message string, args ...any) Error {
func Failw(message string, details ...any) Error { func Failw(message string, details ...any) Error {
return &Err{ return &Err{
origin: errors.New(message), origin: errors.New(message),
details: details,
caller: caller.New(3), caller: caller.New(3),
code: connect.CodeInternal, code: connect.CodeInternal,
details: details,
} }
} }

View file

@ -61,7 +61,7 @@ func ErrorMessageInterceptor() connect.UnaryInterceptorFunc {
if cerr := new(connect.Error); errors.As(err, &cerr) { if cerr := new(connect.Error); errors.As(err, &cerr) {
if e := errs.FindError(cerr); e != nil { if e := errs.FindError(cerr); e != nil {
msg := e.Error() msg := e.GetMessage()
spanContext := span.SpanContext() spanContext := span.SpanContext()
traceId := spanContext.TraceID().String() traceId := spanContext.TraceID().String()
e.Message("%s|%s", traceId, msg) e.Message("%s|%s", traceId, msg)