Skip to content

Commit

Permalink
Tweak errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Mar 8, 2018
1 parent f2d58cd commit 0895007
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
21 changes: 12 additions & 9 deletions urns/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import (

// Simple URN parser loosely based on RFC2141 (https://www.ietf.org/rfc/rfc2141.txt)

const (
stateScheme = iota
statePath
stateQuery
stateFragment
)

var escapes = map[rune]string{
'#': `%23`,
'%': `%25`,
Expand All @@ -39,6 +32,13 @@ func (u *parsedURN) String() string {
return s
}

const (
stateScheme = iota
statePath
stateQuery
stateFragment
)

func parseURN(urn string) (*parsedURN, error) {
state := stateScheme

Expand Down Expand Up @@ -74,8 +74,11 @@ func parseURN(urn string) (*parsedURN, error) {
buffers[state].WriteRune(c)
}

if state == stateScheme || buffers[stateScheme].Len() == 0 {
return nil, fmt.Errorf("must contain at least scheme and path")
if buffers[stateScheme].Len() == 0 {
return nil, fmt.Errorf("scheme cannot be empty")
}
if buffers[statePath].Len() == 0 {
return nil, fmt.Errorf("path cannot be empty")
}

return &parsedURN{
Expand Down
3 changes: 2 additions & 1 deletion urns/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func TestParseURNAndBack(t *testing.T) {
// can't single part
{input: "xyz", hasError: true},

// can't omit scheme
// can't omit scheme or path
{input: ":path", hasError: true},
{input: "scheme:", hasError: true},

// can't have multiple queries or fragments
{input: "scheme:path?query?query", hasError: true},
Expand Down
8 changes: 4 additions & 4 deletions urns/urns.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ func (u URN) Normalize(country string) URN {
// Validate returns whether this URN is considered valid
func (u URN) Validate() error {
scheme, path, _, display := u.ToParts()

if scheme == "" || path == "" {
return fmt.Errorf("scheme or path cannot be empty")
}
if !IsValidScheme(scheme) {
return fmt.Errorf("invalid scheme: '%s'", scheme)
}

if path == "" {
return fmt.Errorf("path cannot be empty")
}

switch scheme {
case TelScheme:
// validate is possible phone number
Expand Down
8 changes: 4 additions & 4 deletions urns/urns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ func TestValidate(t *testing.T) {
urn URN
expectedError string
}{
{"xxxx", "invalid scheme"}, // un-parseable URNs don't validate
{"xyz:abc", "invalid scheme"}, // nor do unknown schemes
{"tel:", "path cannot be empty"},
{"xxxx", "scheme or path cannot be empty"}, // un-parseable URNs don't validate
{"xyz:abc", "invalid scheme"}, // nor do unknown schemes
{"tel:", "scheme or path cannot be empty"},

// valid tel numbers
{"tel:+250788383383", ""},
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestValidate(t *testing.T) {
}

if err != nil && !strings.Contains(err.Error(), tc.expectedError) {
t.Errorf("Failed wrong error, '%s' not found in '%s'", tc.expectedError, err.Error())
t.Errorf("Failed wrong error, '%s' not found in '%s' for '%s'", tc.expectedError, err.Error(), string(tc.urn))
}
}

Expand Down

0 comments on commit 0895007

Please sign in to comment.