Documentation ¶
Index ¶
- func GetFields(err error) map[string]any
- func GetFieldsAsCombinedSlice(err error) []any
- type Error
- type ErrorBuilder
- func (eb *ErrorBuilder) Errorf(format string, a ...any) *Error
- func (eb *ErrorBuilder) New(message string) *Error
- func (eb *ErrorBuilder) With(key string, value any) *ErrorBuilder
- func (eb *ErrorBuilder) Wrap(err error, message string) *Error
- func (eb *ErrorBuilder) Wrapf(err error, format string, a ...any) *Error
- type ErrorStack
- type StackFrame
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetFieldsAsCombinedSlice ¶
GetFieldsAsCombinedSlice will return all fields as a slice that are added to the specified error. The format will be [key1, value1, key2, value2, ..., keyN, valueN].
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is the error that will be returned by ErrorBuilder and the functions New, Errorf, Wrap and Wrapf. It implements the stdlib error interface.
Example ¶
package main import ( "log/slog" "slices" "github.com/Eun/serrors" ) func validateUserNameLength(name string) error { const maxLength = 10 if len(name) > maxLength { return serrors.New("username is too long"). With("username", name). With("max_length", maxLength) } return nil } func validateUserName(name string) error { serr := serrors.NewBuilder(). With("username", name) if name == "" { return serr.New("username cannot be empty") } if err := validateUserNameLength(name); err != nil { return serr.Wrap(err, "username has invalid length") } reservedNames := []string{"root", "admin"} for _, s := range reservedNames { if name == s { return serr.Errorf("username cannot be %q, it is reserved", name). With("reserved", reservedNames) } } return nil } func addUserToRole(userName, roleName string) error { if err := validateUserName(userName); err != nil { return serrors.Wrap(err, "validation of username failed").With("username", userName) } if roleName == "" { return serrors.New("rolename cannot be empty") } availableRoles := []string{"admin", "user"} if !slices.Contains(availableRoles, roleName) { return serrors.Errorf("unknown role %q", roleName). With("username", userName). With("available_roles", availableRoles) } // todo: add user to role // ... return nil } func main() { if err := addUserToRole("joe", "guest"); err != nil { slog.Error("name validation failed", "error", err.Error(), slog.Group("details", serrors.GetFieldsAsCombinedSlice(err)...), "stack", serrors.GetStack(err), ) return } }
Output:
func Errorf ¶
Errorf creates a new Error with the supplied message formatted according to a format specifier.
Example ¶
package main import ( "fmt" "github.com/Eun/serrors" ) func main() { name := "Joe" if name == "alice" { panic(serrors.Errorf("invalid name %q", name).With("name", name)) } fmt.Println(name) }
Output: Joe
func New ¶
New creates a new Error with the supplied message.
Example ¶
package main import ( "fmt" "github.com/Eun/serrors" ) func main() { name := "Joe" if name == "alice" { panic(serrors.New("invalid name").With("name", name)) } fmt.Println(name) }
Output: Joe
func Wrap ¶
Wrap creates a new Error with the supplied message. The passed in error will be added as a cause for this error.
Example ¶
name := "Joe" if err := validateUserName(name); err != nil { panic(serrors.Wrap(err, "invalid name").With("name", name)) } fmt.Println(name)
Output: Joe
func Wrapf ¶
Wrapf creates a new Error with the supplied message formatted according to a format specifier. The passed in error will be added as a cause for this error.
Example ¶
name := "Joe" if err := validateUserName(name); err != nil { panic(serrors.Wrapf(err, "invalid name %q", name).With("name", name)) } fmt.Println(name)
Output: Joe
func (*Error) Error ¶
Error returns the error string representation including the cause of this error.
type ErrorBuilder ¶
type ErrorBuilder struct {
// contains filtered or unexported fields
}
ErrorBuilder is a type that provides a way to build errors.
func NewBuilder ¶
func NewBuilder() *ErrorBuilder
NewBuilder creates a new ErrorBuilder. To build the error use either ErrorBuilder.New, ErrorBuilder.Errorf, ErrorBuilder.Wrap or ErrorBuilder.Wrapf.
Example ¶
package main import ( "fmt" "github.com/Eun/serrors" ) func main() { name := "Joe" serr := serrors.NewBuilder(). With("username", name) if name == "alice" { panic(serr.New("username cannot be alice")) } fmt.Println(name) }
Output: Joe
func (*ErrorBuilder) Errorf ¶
func (eb *ErrorBuilder) Errorf(format string, a ...any) *Error
Errorf creates a new Error with the supplied message formatted according to a format specifier. The error will contain all fields that were previously passed to ErrorBuilder.
Example ¶
package main import ( "fmt" "github.com/Eun/serrors" ) func main() { name := "Joe" serr := serrors.NewBuilder(). With("username", name) if name == "alice" { panic(serr.Errorf("username cannot be %q", name)) } fmt.Println(name) }
Output: Joe
func (*ErrorBuilder) New ¶
func (eb *ErrorBuilder) New(message string) *Error
New creates a new Error with the supplied message. The error will contain all fields that were previously passed to ErrorBuilder.
Example ¶
package main import ( "fmt" "github.com/Eun/serrors" ) func main() { name := "Joe" serr := serrors.NewBuilder(). With("username", name) if name == "alice" { panic(serr.New("username cannot be alice")) } fmt.Println(name) }
Output: Joe
func (*ErrorBuilder) With ¶
func (eb *ErrorBuilder) With(key string, value any) *ErrorBuilder
With adds the field key with the value to the error fields.
Example ¶
package main import ( "fmt" "github.com/Eun/serrors" ) func main() { name := "Joe" serr := serrors.NewBuilder() serr = serr.With("username", name) if name == "alice" { panic(serr.New("username cannot be alice")) } fmt.Println(name) }
Output: Joe
func (*ErrorBuilder) Wrap ¶
func (eb *ErrorBuilder) Wrap(err error, message string) *Error
Wrap creates a new Error with the supplied message. The passed in error will be added as a cause for this error. The error will contain all fields that were previously passed to ErrorBuilder.
Example ¶
name := "Joe" serr := serrors.NewBuilder(). With("username", name) if err := validateUserName(name); err != nil { panic(serr.Wrap(err, "validation of username failed")) } fmt.Println(name)
Output: Joe
func (*ErrorBuilder) Wrapf ¶
func (eb *ErrorBuilder) Wrapf(err error, format string, a ...any) *Error
Wrapf creates a new Error with the supplied message formatted according to a format specifier. The passed in error will be added as a cause for this error. The error will contain all fields that were previously passed to ErrorBuilder.
Example ¶
name := "Joe" serr := serrors.NewBuilder(). With("username", name) if err := validateUserName(name); err != nil { panic(serr.Wrapf(err, "validation of username %q failed", name)) } fmt.Println(name)
Output: Joe
type ErrorStack ¶
type ErrorStack struct { ErrorMessage string `json:"error_message" yaml:"error_message"` Fields map[string]any `json:"fields" yaml:"fields"` StackTrace []StackFrame `json:"stack_trace" yaml:"stack_trace"` // contains filtered or unexported fields }
ErrorStack holds an error and its relevant information.
func GetStack ¶
func GetStack(err error) []ErrorStack
GetStack returns the errors that are present in the provided error.
type StackFrame ¶
type StackFrame struct { File string `json:"file" yaml:"file"` Func string `json:"func" yaml:"func"` Line int `json:"line" yaml:"line"` }
StackFrame represents a single stack frame of an error.