Documentation
¶
Index ¶
- func HasTag(err error, tags ...any) bool
- func IsUserError(err error) bool
- func LookupContext(err error) (context.Context, bool)
- func LookupDetail(err error) (string, bool)
- func Merge(errs ...error) error
- func Tag[Tag ~struct{}](err error, tag Tag) error
- type Error
- type ErrorHandler
- type UserError
- type WithErr
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HasTag ¶
Example ¶
package main import ( "fmt" "github.com/adamluzsi/frameless/pkg/errorutil" ) func main() { type MyTag struct{} err := fmt.Errorf("foo bar baz") errorutil.HasTag(err, MyTag{}) // false err = errorutil.Tag(err, MyTag{}) errorutil.HasTag(err, MyTag{}) // true }
Output:
func IsUserError ¶
Example ¶
package main import ( "fmt" "github.com/adamluzsi/frameless/pkg/errorutil" ) func main() { err := errorutil.UserError{ ID: "constant-err-scenario-code", Message: "some message for the dev", } if errorutil.IsUserError(err) { fmt.Println("it's a Layer 8 error") } }
Output:
func LookupDetail ¶
func Merge ¶
Merge will combine all given non nil error values into a single error value. If no valid error is given, nil is returned. If only a single non nil error value is given, the error value is returned.
Example ¶
package main import ( "fmt" "github.com/adamluzsi/frameless/pkg/errorutil" ) func main() { // creates an error value that combines the input errors. err := errorutil.Merge(fmt.Errorf("foo"), fmt.Errorf("bar"), nil) _ = err }
Output:
Types ¶
type Error ¶
type Error string
Error is an implementation for the error interface that allow you to declare exported globals with the `const` keyword.
TL;DR: const ErrSomething errorutil.Error = "something is an error"
type ErrorHandler ¶
type ErrorHandler interface { // HandleError allow the interactor implementation to be notified about unexpected situations. HandleError(ctx context.Context, err error) error }
ErrorHandler describes that an object able to handle error use-cases for its purpose.
For e.g. if the component is a pubsub subscription event handler, then implementing ErrorHandler means it suppose to handle unexpected use-cases such as connection interruption.
type UserError ¶
type UserError struct { // ID is a constant string value that expresses the user's error scenario. // The caller who receives the error will use this code to present the UserError to their users and, // most likely, provide a localised error message about the error scenario to the end user. // Traditionally this should be a string without any white space. // // Example: "foo-is-forbidden-with-active-baz" ID constant.String // Message is the error message meant to be read by a developer working on the implementation of the caller. // It is not expected to be seen by end users. // It might be written in English for portability reasons. // // Example: "Authentication failed due to incorrect username or password." Message constant.String }
Example ¶
package main import ( "errors" "fmt" "github.com/adamluzsi/frameless/pkg/errorutil" "math/rand" ) func main() { fooEntityID := rand.Int() bazEntityID := rand.Int() usrerr := errorutil.UserError{ ID: "foo-is-forbidden-with-active-baz", Message: "It is forbidden to execute Foo when you have an active Baz", } var err error = usrerr.With().Detailf("Foo(ID:%d) /Baz(ID:%d)", fooEntityID, bazEntityID) // add some details using error wrapping err = fmt.Errorf("some wrapper layer: %w", err) // retrieve with errors pkg if ue := (errorutil.UserError{}); errors.As(err, &ue) { fmt.Printf("%#v\n", ue) } if errors.Is(err, errorutil.UserError{}) { fmt.Println("it's a Layer 8 error") } // retrieve with errorutil pkg if userError, ok := errorutil.LookupUserError(err); ok { fmt.Printf("%#v\n", userError) } if errorutil.IsUserError(err) { fmt.Println("it's a Layer 8 error") } }
Output:
func LookupUserError ¶
Example ¶
package main import ( "fmt" "github.com/adamluzsi/frameless/pkg/errorutil" ) func main() { err := errorutil.UserError{ ID: "constant-err-scenario-code", Message: "some message for the dev", } if userError, ok := errorutil.LookupUserError(err); ok { fmt.Printf("%#v\n", userError) } }
Output:
func (UserError) With ¶
Example ¶
package main import ( "github.com/adamluzsi/frameless/pkg/errorutil" ) func main() { usrErr := errorutil.UserError{ ID: "foo-is-forbidden-with-active-baz", Message: "It is forbidden to execute Foo when you have an active Baz", } // returns with Err that has additional concrete details _ = usrErr.With().Detailf("Foo(ID:%d) /Baz(ID:%d)", 42, 7) }
Output:
type WithErr ¶ added in v0.90.0
type WithErr struct{ Err error }
func (WithErr) Context ¶ added in v0.90.0
Context will combine an error with a context, so the current context can be used at the place of error handling. This can be useful if tracing ID and other helpful values are kept in the context.
func (WithErr) Detail ¶ added in v0.90.0
Detail will return an error that has explanation as detail attached to it.