Skip to content

Commit

Permalink
Add new error when int parsing fails
Browse files Browse the repository at this point in the history
It's hard to reason with errors when `strconv.ParseUint` fails as
the returned error is untyped. This change introduces a new error
that is returned if the int parsing fails.

Signed-off-by: crozzy <[email protected]>
  • Loading branch information
crozzy committed Oct 9, 2023
1 parent 2f39fdc commit a24cef3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
10 changes: 7 additions & 3 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ var (

// ErrInvalidPrerelease is returned when the pre-release is an invalid format
ErrInvalidPrerelease = errors.New("Invalid Prerelease string")

// ErrSegmentUnparsable is returned if a version segment cannot be parsed as
// an int.
ErrSegmentUnparsable = errors.New("Version segment unparsable")
)

// semVerRegex is the regular expression used to parse a semantic version.
Expand Down Expand Up @@ -170,13 +174,13 @@ func NewVersion(v string) (*Version, error) {
var err error
sv.major, err = strconv.ParseUint(m[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("Error parsing version segment: %s", err)
return nil, ErrSegmentUnparsable
}

if m[2] != "" {
sv.minor, err = strconv.ParseUint(strings.TrimPrefix(m[2], "."), 10, 64)
if err != nil {
return nil, fmt.Errorf("Error parsing version segment: %s", err)
return nil, ErrSegmentUnparsable
}
} else {
sv.minor = 0
Expand All @@ -185,7 +189,7 @@ func NewVersion(v string) (*Version, error) {
if m[3] != "" {
sv.patch, err = strconv.ParseUint(strings.TrimPrefix(m[3], "."), 10, 64)
if err != nil {
return nil, fmt.Errorf("Error parsing version segment: %s", err)
return nil, ErrSegmentUnparsable
}
} else {
sv.patch = 0
Expand Down
30 changes: 30 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package semver
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"strconv"
"testing"
)

Expand Down Expand Up @@ -162,6 +164,34 @@ func TestOriginal(t *testing.T) {
}
}

func TestErrors(t *testing.T) {
tests := map[string]error{
"1.3.2023080721291691443750": ErrSegmentUnparsable,
"1.2.3.4.5": ErrInvalidSemVer,
"": ErrInvalidSemVer,
"1.2.0+invalid_metadata": ErrInvalidSemVer,
}
for v, e := range tests {
_, err := NewVersion(v)
if !errors.Is(err, e) {
t.Errorf("Expecting error: %s but got: %s, version %s", e, err, v)
}
}

strictTests := map[string]error{
"1.3.2023080721291691443750": strconv.ErrRange,
"1.2.3.4.5": ErrInvalidCharacters,
"": ErrEmptyString,
"1.2.0+invalid$metadata": ErrInvalidMetadata,
}
for v, e := range strictTests {
_, err := StrictNewVersion(v)
if !errors.Is(err, e) {
t.Errorf("Expecting error: %s but got: %s, version: %s", e, err, v)
}
}
}

func TestParts(t *testing.T) {
v, err := NewVersion("1.2.3-beta.1+build.123")
if err != nil {
Expand Down

0 comments on commit a24cef3

Please sign in to comment.