Skip to content

Commit

Permalink
Satisfy errors.Is even if formatting is incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
barrettj12 committed Sep 26, 2023
1 parent 54a9448 commit 5384712
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ _testmain.go

*.exe
*.test

# IDE files
/.idea/
30 changes: 15 additions & 15 deletions errortypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func WithType(err error, errType ConstError) error {
// interface.
func Timeoutf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Timeout, format, args...),
WithType(makeWrappedConstError(Timeout, format, args...), Timeout),
1,
)
}
Expand All @@ -140,7 +140,7 @@ func IsTimeout(err error) bool {
// Locationer interface.
func NotFoundf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotFound, format, args...),
WithType(makeWrappedConstError(NotFound, format, args...), NotFound),
1,
)
}
Expand All @@ -164,7 +164,7 @@ func IsNotFound(err error) bool {
// Locationer interface.
func UserNotFoundf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(UserNotFound, format, args...),
WithType(makeWrappedConstError(UserNotFound, format, args...), UserNotFound),
1,
)
}
Expand All @@ -188,7 +188,7 @@ func IsUserNotFound(err error) bool {
// the Locationer interface.
func Unauthorizedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(Unauthorized), format, args...),
WithType(makeWrappedConstError(Hide(Unauthorized), format, args...), Unauthorized),
1,
)
}
Expand All @@ -212,7 +212,7 @@ func IsUnauthorized(err error) bool {
// the Locationer interface.
func NotImplementedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotImplemented, format, args...),
WithType(makeWrappedConstError(NotImplemented, format, args...), NotImplemented),
1,
)
}
Expand All @@ -236,7 +236,7 @@ func IsNotImplemented(err error) bool {
// the Locationer interface.
func AlreadyExistsf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(AlreadyExists, format, args...),
WithType(makeWrappedConstError(AlreadyExists, format, args...), AlreadyExists),
1,
)
}
Expand All @@ -260,7 +260,7 @@ func IsAlreadyExists(err error) bool {
// Locationer interface.
func NotSupportedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotSupported, format, args...),
WithType(makeWrappedConstError(NotSupported, format, args...), NotSupported),
1,
)
}
Expand All @@ -284,7 +284,7 @@ func IsNotSupported(err error) bool {
// Locationer interface.
func NotValidf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotValid, format, args...),
WithType(makeWrappedConstError(NotValid, format, args...), NotValid),
1,
)
}
Expand All @@ -308,7 +308,7 @@ func IsNotValid(err error) bool {
// the Locationer interface.
func NotProvisionedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotProvisioned, format, args...),
WithType(makeWrappedConstError(NotProvisioned, format, args...), NotProvisioned),
1,
)
}
Expand All @@ -332,7 +332,7 @@ func IsNotProvisioned(err error) bool {
// Locationer interface.
func NotAssignedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotAssigned, format, args...),
WithType(makeWrappedConstError(NotAssigned, format, args...), NotAssigned),
1,
)
}
Expand All @@ -356,7 +356,7 @@ func IsNotAssigned(err error) bool {
// Locationer interface.
func BadRequestf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(BadRequest), format, args...),
WithType(makeWrappedConstError(Hide(BadRequest), format, args...), BadRequest),
1,
)
}
Expand All @@ -380,7 +380,7 @@ func IsBadRequest(err error) bool {
// and the Locationer interface.
func MethodNotAllowedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(MethodNotAllowed), format, args...),
WithType(makeWrappedConstError(Hide(MethodNotAllowed), format, args...), MethodNotAllowed),
1,
)
}
Expand All @@ -404,7 +404,7 @@ func IsMethodNotAllowed(err error) bool {
// Locationer interface.
func Forbiddenf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(Forbidden), format, args...),
WithType(makeWrappedConstError(Hide(Forbidden), format, args...), Forbidden),
1,
)
}
Expand All @@ -428,7 +428,7 @@ func IsForbidden(err error) bool {
// Is(err, QuotaLimitExceeded) and the Locationer interface.
func QuotaLimitExceededf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(QuotaLimitExceeded), format, args...),
WithType(makeWrappedConstError(Hide(QuotaLimitExceeded), format, args...), QuotaLimitExceeded),
1,
)
}
Expand All @@ -452,7 +452,7 @@ func IsQuotaLimitExceeded(err error) bool {
// and the Locationer interface.
func NotYetAvailablef(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(NotYetAvailable), format, args...),
WithType(makeWrappedConstError(Hide(NotYetAvailable), format, args...), NotYetAvailable),
1,
)
}
Expand Down
26 changes: 26 additions & 0 deletions errortypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,29 @@ func (*errorTypeSuite) TestWithType(c *gc.C) {
c.Assert(err.Error(), gc.Equals, "yes")
c.Assert(errors.Is(err, myErr2), gc.Equals, false)
}

func (*errorTypeSuite) TestBadFormatNotEnoughArgs(c *gc.C) {
errorTests := make([]errorTest, 0, len(allErrors))
for _, errInfo := range allErrors {
errorTests = append(errorTests, errorTest{
errInfo.argsConstructor("missing arg %v"),
".*",
errInfo,
})
}

runErrorTests(c, errorTests, true)
}

func (*errorTypeSuite) TestBadFormatTooManyArgs(c *gc.C) {
errorTests := make([]errorTest, 0, len(allErrors))
for _, errInfo := range allErrors {
errorTests = append(errorTests, errorTest{
errInfo.argsConstructor("extra arg %v", "foo", "bar"),
".*",
errInfo,
})
}

runErrorTests(c, errorTests, true)
}

0 comments on commit 5384712

Please sign in to comment.