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.
//
// 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 {
s := strings.Builder{}
if e.message != "" {
s.WriteString(e.message)
if e.origin != nil {
s.WriteString(": ")
}
}
unwrap := errors.Unwrap(e)
for unwrap != nil {
var current string
if e, ok := unwrap.(Error); ok {
if msg := e.GetMessage(); msg != "" {
current = msg
}
if e, ok := unwrap.(Error); ok && e.GetMessage() != "" {
current = e.GetMessage()
} else {
current = unwrap.Error()
}
next := errors.Unwrap(unwrap)
if next != nil {
current, _ = strings.CutSuffix(current, next.Error())
current, _ = strings.CutSuffix(current, ": ")
}
if current != "" {
if _, ok := next.(Error); ok {
s.WriteString(current)
if next != nil {
s.WriteString(": ")
}
s.WriteString(current)
}
unwrap = next
@ -223,9 +222,9 @@ func Failf(message string, args ...any) Error {
func Failw(message string, details ...any) Error {
return &Err{
origin: errors.New(message),
details: details,
caller: caller.New(3),
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 e := errs.FindError(cerr); e != nil {
msg := e.Error()
msg := e.GetMessage()
spanContext := span.SpanContext()
traceId := spanContext.TraceID().String()
e.Message("%s|%s", traceId, msg)