improve version error formatting

This commit is contained in:
Aneurin Barker Snook
2024-07-11 22:34:57 +01:00
parent e69de8b117
commit 6b77391d28
4 changed files with 44 additions and 14 deletions
+37
View File
@@ -0,0 +1,37 @@
package version
import "fmt"
// Error represents a version error.
type Error struct {
Message string
Version string
}
// Version error.
var (
ErrInvalidVersion = Error{Message: "invalid version %q"}
)
// Error retrieves the message of a REST API error.
func (e Error) Error() string {
return fmt.Sprintf(e.Message, e.Version)
}
// Is determines whether the Error is an instance of the target.
// https://pkg.go.dev/errors#Is
//
// This implementation does not compare versions.
func (e Error) Is(target error) bool {
if t, ok := target.(Error); ok {
return t.Message == e.Message
}
return false
}
func invalid(version string) Error {
return Error{
Message: ErrInvalidVersion.Message,
Version: version,
}
}