Skip to content

Commit

Permalink
add easier switch checking
Browse files Browse the repository at this point in the history
  • Loading branch information
damienfamed75 committed Jun 4, 2020
1 parent fa94e0b commit fc56d00
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
11 changes: 7 additions & 4 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
// NewError returns a new core error with an identifier.
// The core can be used as a standard error like when using errors.New or
// they can be used with the custom Errorf to categorize other errors.
func NewError(identity string) *y.Core {
func NewError(identity string) y.Identity {
return y.NewError(identity)
}

// Errorf returns a new custom error with a particular type.
func Errorf(c *y.Core, format string, args ...interface{}) error {
cc := *c
cc.Fill(format, args...)
func Errorf(identity y.Identity, format string, args ...interface{}) error {
cc := y.Core{}
cc.Fill(identity, format, args...)
return &cc
}

// Core is an export of the error core.
type Core = y.Core
42 changes: 32 additions & 10 deletions internal/y/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,46 @@ var (
// Core is a general purpose error with a given identifier to help
// categorize and compare between different errors.
type Core struct {
identity string
identity Identity
err error
}

// Fill sets the value of the core's error with a wrapped error.
func (c *Core) Fill(format string, args ...interface{}) {
func (c *Core) Fill(identity Identity, format string, args ...interface{}) {
c.identity = identity
c.err = fmt.Errorf(format, args...)
}

// Is compares other Core errors and checks their identifiers.
func (c *Core) Is(target error) bool {
if tCore, ok := target.(*Core); ok {
if c.identity == tCore.identity {
return true
for {
// Check if the error is an identity.
var ident Identity
if errors.As(target, &ident) {
return ident == c.identity
}
// Check if the error is a core.
tmpCore := &Core{}
if errors.As(target, &tmpCore) {
return tmpCore.identity == c.identity
}
// Try to unwrap the error.
if target = errors.Unwrap(target); target == nil {
return false
}
}

return false
}

func (c *Core) Error() string {
if c.err != nil {
return c.err.Error()
}

return c.identity.Error()
}

// Identity returns the identity of the core.
func (c *Core) Identity() Identity {
return c.identity
}

Expand All @@ -47,7 +62,14 @@ func (c *Core) Unwrap() error {
return errors.Unwrap(c.err)
}

// NewError returns a core error with an identifier.
func NewError(identity string) *Core {
return &Core{identity: identity}
// Identity is a barebones string error, and can be paired with a core.
type Identity string

func (i Identity) Error() string {
return string(i)
}

// NewError returns the identity of a new error.
func NewError(identity string) Identity {
return Identity(identity)
}

0 comments on commit fc56d00

Please sign in to comment.