File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ package logging
2+
3+ import (
4+ "github.com/pkg/errors"
5+ "go.uber.org/zap"
6+ )
7+
8+ // stackTracer is an interface used to identify errors that include a stack trace.
9+ // This interface specifically targets errors created using the github.com/pkg/errors library,
10+ // which can add stack traces to errors with functions like errors.Wrap().
11+ type stackTracer interface {
12+ StackTrace () errors.StackTrace
13+ }
14+
15+ // errNoStackTrace is a wrapper for errors that implements the error interface without exposing a stack trace.
16+ type errNoStackTrace struct {
17+ e error
18+ }
19+
20+ // Error returns the error message of the wrapped error.
21+ func (e errNoStackTrace ) Error () string {
22+ return e .e .Error ()
23+ }
24+
25+ // Error returns a zap.Field for logging the provided error.
26+ // This function checks if the error includes a stack trace from the pkg/errors library.
27+ // If a stack trace is present, it is suppressed in the log output because
28+ // logging a stack trace is not necessary. Otherwise, the error is logged normally.
29+ func Error (e error ) zap.Field {
30+ if _ , ok := e .(stackTracer ); ok {
31+ return zap .Error (errNoStackTrace {e })
32+ }
33+
34+ return zap .Error (e )
35+ }
You can’t perform that action at this time.
0 commit comments