diff --git a/internal/transport/error.go b/internal/transport/error.go index b4d3d79..c2a7b00 100644 --- a/internal/transport/error.go +++ b/internal/transport/error.go @@ -98,6 +98,10 @@ func (e *transportError) WithCauses(causes ...zeerr.Error) zeerr.Error { return e } +func (e *transportError) WithInternalErr(_ error) zeerr.Error { + return e +} + func (e transportError) Error() string { return e.internalMsg } diff --git a/zeerr/error.go b/zeerr/error.go index 3e064f7..83e348c 100644 --- a/zeerr/error.go +++ b/zeerr/error.go @@ -46,6 +46,10 @@ type Error interface { Causes() []Error // WithCauses is used to attach causes to the error. WithCauses(causes ...Error) Error + // WithInternalErr is used to attach internal error to the error. + // Internal errors are not exposed to the client and are used for + // debugging purposes. + WithInternalErr(internalErr error) Error // Error implements the error interface. Error() string } diff --git a/zeerr/standard_error.go b/zeerr/standard_error.go index 3b2c6ed..cab0e14 100644 --- a/zeerr/standard_error.go +++ b/zeerr/standard_error.go @@ -24,6 +24,8 @@ type standardError struct { publicMsg string causes []Error + + internalErr error } // NewError creates a new error. @@ -118,6 +120,11 @@ func (e standardError) Causes() []Error { } // WithCauses is used to attach causes to the error. +// Causes should be of type Error and it's used to create +// complex nested error structures. +// +// Causes will not be unwrapped by the standard error implementation. +// To include internal error use WithInternalErr instead. func (e *standardError) WithCauses(causes ...Error) Error { for _, c := range causes { if c != nil { @@ -128,6 +135,20 @@ func (e *standardError) WithCauses(causes ...Error) Error { return e } +// WithInternalErr is used to attach an internal error to the error. +// This error will be unwrapped by the standard error implementation. +// Internal error will not be included in the encoded transport error. +func (e *standardError) WithInternalErr(internalErr error) Error { + e.internalErr = internalErr + + return e +} + +// Unwrap returns an internal error. +func (e *standardError) Unwrap() error { + return e.internalErr +} + // Error implements the error interface. func (e *standardError) Error() string { return e.formattedErr() @@ -142,5 +163,11 @@ func (e *standardError) formattedErr() string { buf.WriteString(cause.Error()) } + if e.internalErr != nil { + buf.WriteString("\n\t") + buf.WriteString("internal error: ") + buf.WriteString(e.internalErr.Error()) + } + return buf.String() }