From fc56d0063efbba614c4d8aa53a797100830d7c7f Mon Sep 17 00:00:00 2001 From: damienfamed75 Date: Thu, 4 Jun 2020 18:44:43 -0500 Subject: [PATCH] add easier switch checking --- core.go | 11 +++++++---- internal/y/core.go | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/core.go b/core.go index 1c7dff7..5eb7221 100644 --- a/core.go +++ b/core.go @@ -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 diff --git a/internal/y/core.go b/internal/y/core.go index cc195c6..c01d6a9 100644 --- a/internal/y/core.go +++ b/internal/y/core.go @@ -14,24 +14,34 @@ 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 { @@ -39,6 +49,11 @@ func (c *Core) Error() string { return c.err.Error() } + return c.identity.Error() +} + +// Identity returns the identity of the core. +func (c *Core) Identity() Identity { return c.identity } @@ -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) }