Browse Source

Send nice error when version number is out of range

master
GeertJohan 10 years ago
parent
commit
f4151cb744
2 changed files with 12 additions and 4 deletions
  1. +4
    -0
      main.go
  2. +8
    -4
      version.go

+ 4
- 0
main.go View File

@@ -138,6 +138,10 @@ func handler(resp http.ResponseWriter, req *http.Request) {
var ok bool
repo.Version, ok = parseVersion(m[3])
if !ok {
if repo.Version == TooHighVersion {
sendNotFound(resp, "Version %q contains a number which is out of range.", m[3])
return
}
sendNotFound(resp, "Version %q improperly considered invalid; please warn the service maintainers.", m[3])
return
}


+ 8
- 4
version.go View File

@@ -48,6 +48,7 @@ func (v Version) IsValid() bool {
}

var InvalidVersion = Version{-1, -1, -1}
var TooHighVersion = Version{-2, -2, -2}

func parseVersion(s string) (Version, bool) {
if len(s) < 2 {
@@ -66,17 +67,20 @@ func parseVersion(s string) (Version, bool) {
if len(part) == 0 || part[0] == '0' {
return InvalidVersion, false
}
num, err := strconv.Atoi(part)
num, err := strconv.ParseInt(part, 10, 32)
if err != nil {
if err.(*strconv.NumError).Err == strconv.ErrRange {
return TooHighVersion, false
}
return InvalidVersion, false
}
switch i {
case 0:
v.Major = num
v.Major = int(num)
case 1:
v.Minor = num
v.Minor = int(num)
case 2:
v.Patch = num
v.Patch = int(num)
}
}



Loading…
Cancel
Save