Skip to content

Commit

Permalink
feat: add code to errs.PublicError
Browse files Browse the repository at this point in the history
  • Loading branch information
gazenw committed Nov 26, 2024
1 parent 920f7fe commit 58f8497
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions common/errs/public_errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type PublicError struct {
err error
message string
code string // code is optional, it can be used to identify the error type
}

func (p PublicError) Error() string {
Expand All @@ -21,6 +22,10 @@ func (p PublicError) Message() string {
return p.message
}

func (p PublicError) Code() string {
return p.code
}

func (p PublicError) Unwrap() error {
return p.err
}
Expand All @@ -29,6 +34,10 @@ func NewPublicError(message string) error {
return withstack.WithStackDepth(&PublicError{err: errors.New(message), message: message}, 1)
}

func NewPublicErrorWithCode(message string, code string) error {
return withstack.WithStackDepth(&PublicError{err: errors.New(message), message: message, code: code}, 1)
}

func WithPublicMessage(err error, prefix string) error {
if err == nil {
return nil
Expand All @@ -41,3 +50,16 @@ func WithPublicMessage(err error, prefix string) error {
}
return withstack.WithStackDepth(&PublicError{err: err, message: message}, 1)
}

func WithPublicMessageCode(err error, prefix string, code string) error {
if err == nil {
return nil
}
var message string
if prefix != "" {
message = fmt.Sprintf("%s: %s", prefix, err.Error())
} else {
message = err.Error()
}
return withstack.WithStackDepth(&PublicError{err: err, message: message, code: code}, 1)
}

0 comments on commit 58f8497

Please sign in to comment.