Skip to content

Commit

Permalink
add WithInternalErr method
Browse files Browse the repository at this point in the history
  • Loading branch information
amanbolat committed Jun 6, 2024
1 parent 577c359 commit c8151e3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/transport/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions zeerr/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
27 changes: 27 additions & 0 deletions zeerr/standard_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type standardError struct {
publicMsg string

causes []Error

internalErr error
}

// NewError creates a new error.
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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()
}

0 comments on commit c8151e3

Please sign in to comment.