Merge pull request #1048 from estesp/err-cause-withstack-fixes

Error handler cleanup
This commit is contained in:
Michael Crosby 2017-06-21 17:18:34 -07:00 committed by GitHub
commit 3c9c46e2e3
2 changed files with 28 additions and 72 deletions

View File

@ -1,5 +1,7 @@
package content package content
import "github.com/pkg/errors"
type contentExistsErr struct { type contentExistsErr struct {
desc string desc string
} }
@ -17,9 +19,9 @@ func ErrExists(msg string) error {
if msg == "" { if msg == "" {
msg = "content: exists" msg = "content: exists"
} }
return contentExistsErr{ return errors.WithStack(contentExistsErr{
desc: msg, desc: msg,
} })
} }
// ErrNotFound is returned when an item is not found. // ErrNotFound is returned when an item is not found.
@ -27,9 +29,9 @@ func ErrNotFound(msg string) error {
if msg == "" { if msg == "" {
msg = "content: not found" msg = "content: not found"
} }
return contentNotFoundErr{ return errors.WithStack(contentNotFoundErr{
desc: msg, desc: msg,
} })
} }
// ErrLocked is returned when content is actively being uploaded, this // ErrLocked is returned when content is actively being uploaded, this
@ -38,9 +40,9 @@ func ErrLocked(msg string) error {
if msg == "" { if msg == "" {
msg = "content: locked" msg = "content: locked"
} }
return contentLockedErr{ return errors.WithStack(contentLockedErr{
desc: msg, desc: msg,
} })
} }
func (c contentExistsErr) Error() string { func (c contentExistsErr) Error() string {
@ -67,54 +69,30 @@ func (c contentLockedErr) Locked() bool {
// IsNotFound returns true if the error is due to a not found content item // IsNotFound returns true if the error is due to a not found content item
func IsNotFound(err error) bool { func IsNotFound(err error) bool {
if err, ok := err.(interface { if err, ok := errors.Cause(err).(interface {
NotFound() bool NotFound() bool
}); ok { }); ok {
return err.NotFound() return err.NotFound()
} }
causal, ok := err.(interface {
Cause() error
})
if !ok {
return false return false
} }
return IsNotFound(causal.Cause())
}
// IsExists returns true if the error is due to an already existing content item // IsExists returns true if the error is due to an already existing content item
func IsExists(err error) bool { func IsExists(err error) bool {
if err, ok := err.(interface { if err, ok := errors.Cause(err).(interface {
Exists() bool Exists() bool
}); ok { }); ok {
return err.Exists() return err.Exists()
} }
causal, ok := err.(interface {
Cause() error
})
if !ok {
return false return false
} }
return IsExists(causal.Cause())
}
// IsLocked returns true if the error is due to a currently locked content item // IsLocked returns true if the error is due to a currently locked content item
func IsLocked(err error) bool { func IsLocked(err error) bool {
if err, ok := err.(interface { if err, ok := errors.Cause(err).(interface {
Locked() bool Locked() bool
}); ok { }); ok {
return err.Locked() return err.Locked()
} }
causal, ok := err.(interface {
Cause() error
})
if !ok {
return false return false
} }
return IsLocked(causal.Cause())
}

View File

@ -1,5 +1,7 @@
package metadata package metadata
import "github.com/pkg/errors"
type metadataExistsErr struct { type metadataExistsErr struct {
desc string desc string
} }
@ -15,9 +17,9 @@ func ErrExists(msg string) error {
if msg == "" { if msg == "" {
msg = "metadata: exists" msg = "metadata: exists"
} }
return metadataExistsErr{ return errors.WithStack(metadataExistsErr{
desc: msg, desc: msg,
} })
} }
// ErrNotFound is returned when an item cannot be found in metadata // ErrNotFound is returned when an item cannot be found in metadata
@ -25,9 +27,9 @@ func ErrNotFound(msg string) error {
if msg == "" { if msg == "" {
msg = "metadata: not found" msg = "metadata: not found"
} }
return metadataNotFoundErr{ return errors.WithStack(metadataNotFoundErr{
desc: msg, desc: msg,
} })
} }
// ErrNotEmpty is returned when a metadata item can't be deleted because it is not empty // ErrNotEmpty is returned when a metadata item can't be deleted because it is not empty
@ -35,9 +37,9 @@ func ErrNotEmpty(msg string) error {
if msg == "" { if msg == "" {
msg = "metadata: namespace not empty" msg = "metadata: namespace not empty"
} }
return metadataNotEmptyErr{ return errors.WithStack(metadataNotEmptyErr{
desc: msg, desc: msg,
} })
} }
func (m metadataExistsErr) Error() string { func (m metadataExistsErr) Error() string {
@ -64,54 +66,30 @@ func (m metadataNotEmptyErr) NotEmpty() bool {
// IsNotFound returns true if the error is due to a missing metadata item // IsNotFound returns true if the error is due to a missing metadata item
func IsNotFound(err error) bool { func IsNotFound(err error) bool {
if err, ok := err.(interface { if err, ok := errors.Cause(err).(interface {
NotFound() bool NotFound() bool
}); ok { }); ok {
return err.NotFound() return err.NotFound()
} }
causal, ok := err.(interface {
Cause() error
})
if !ok {
return false return false
} }
return IsNotFound(causal.Cause())
}
// IsExists returns true if the error is due to an already existing metadata item // IsExists returns true if the error is due to an already existing metadata item
func IsExists(err error) bool { func IsExists(err error) bool {
if err, ok := err.(interface { if err, ok := errors.Cause(err).(interface {
Exists() bool Exists() bool
}); ok { }); ok {
return err.Exists() return err.Exists()
} }
causal, ok := err.(interface {
Cause() error
})
if !ok {
return false return false
} }
return IsExists(causal.Cause())
}
// IsNotEmpty returns true if the error is due to delete request of a non-empty metadata item // IsNotEmpty returns true if the error is due to delete request of a non-empty metadata item
func IsNotEmpty(err error) bool { func IsNotEmpty(err error) bool {
if err, ok := err.(interface { if err, ok := errors.Cause(err).(interface {
NotEmpty() bool NotEmpty() bool
}); ok { }); ok {
return err.NotEmpty() return err.NotEmpty()
} }
causal, ok := err.(interface {
Cause() error
})
if !ok {
return false return false
} }
return IsNotEmpty(causal.Cause())
}